[英]How can I make ListView work with ArrayAdapter in android
I have been stacked for hours trying to solve that problem. 我已经被堆积了几个小时试图解决这个问题。 I will explain my code structure.
我将解释我的代码结构。
I have two classes TwitterActivity
(the main activity) and SearchTwitter
. 我有两个类
TwitterActivity
(主要活动)和SearchTwitter
。 In TwitterActivity
I sign in and then start the SearchTwitter
activity. 在
TwitterActivity
我登录,然后启动SearchTwitter
活动。 I have also their xml layout files, activity_twitter.xml
and search.xml
. 我也有他们的xml布局文件,
activity_twitter.xml
和search.xml
。
The classes look like: 这些类如下所示:
public class TwitterActivity extends Activity
{
private Button mSignin;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter);
mSignin = (Button)findViewById(R.id.login_id);
mSignin.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//startSiginingProcess(...)
Intent intent = new Intent(TwitterActivity.this, SearchTwitter.class);
startActivity(intent);
}
}
});
}
The activity_twitter.xml
has just a single button, Sign In button. activity_twitter.xml
只有一个按钮,即“登录”按钮。 The SearchTwitter
activity looks like: SearchTwitter
活动如下所示:
public class SearchTwitter extends ListActivity {
private Button buttonLogout;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
initializeComponent();
initControl();
/********************************************************/
ArrayList<String> events = new ArrayList<String>();
events.add("Item1");
events.add("Item2");
ListView ls = (ListView) findViewById(R.id.list);
ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, events));
/********************************************************/
}
private void initControl()
{
//Finish Signing procces
}
private void initializeComponent(){
buttonLogout = (Button) findViewById(R.id.buttonLogout);
buttonLogout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//Just LogOut
});
}
And search.xml
is: search.xml
是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2175B0">
<Button
android:id="@+id/buttonLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/logout" />
<ListView
android:id = "@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/buttonLogout" />
<TextView
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
WHAT IS MY PROBLEM NOW 我现在有什么问题
In the SearchTwitter
activity I did extend ListActivity
在
SearchTwitter
活动中,我确实扩展了ListActivity
All I want to do is to make the code surrounded by stars in SearchTwitter
work. 我要做的就是使
SearchTwitter
星号包围的代码SearchTwitter
工作。 I have tried lots of ways, but No result. 我尝试了很多方法,但没有结果。 My app always crashed.
我的应用程序总是崩溃。
I feel exhausted, I don't know what to do anymore. 我感到精疲力尽,我不知道该怎么办了。 Feel free to ask any question.
随时问任何问题。 Any kind of help, suggestion is highly appreciated.
任何帮助,建议都将受到高度赞赏。 Thanks.
谢谢。
PS: The code surrounded by stars is wrong. PS:星号周围的代码是错误的。
EDIT 编辑
In the case ListView ls = (ListView) findViewById(R.layout.search);
如果是
ListView ls = (ListView) findViewById(R.layout.search);
I get: 我得到:
06-22 04:14:39.648: E/AndroidRuntime(1861): FATAL EXCEPTION: main
06-22 04:14:39.648: E/AndroidRuntime(1861): Process: com.bledi.android.twittertest, PID: 1861
06-22 04:14:39.648: E/AndroidRuntime(1861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bledi.android.twittertest/com.bledi.android.twittertest.SearchTwitter}: java.lang.NullPointerException
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.os.Handler.dispatchMessage(Handler.java:102)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.os.Looper.loop(Looper.java:136)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-22 04:14:39.648: E/AndroidRuntime(1861): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 04:14:39.648: E/AndroidRuntime(1861): at java.lang.reflect.Method.invoke(Method.java:515)
06-22 04:14:39.648: E/AndroidRuntime(1861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-22 04:14:39.648: E/AndroidRuntime(1861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-22 04:14:39.648: E/AndroidRuntime(1861): at dalvik.system.NativeStart.main(Native Method)
06-22 04:14:39.648: E/AndroidRuntime(1861): Caused by: java.lang.NullPointerException
06-22 04:14:39.648: E/AndroidRuntime(1861): at com.bledi.android.twittertest.SearchTwitter.onCreate(SearchTwitter.java:38)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.Activity.performCreate(Activity.java:5231)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-22 04:14:39.648: E/AndroidRuntime(1861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
EDIT 2 编辑2
I changed ListView ls = (ListView) findViewById(R.id.list);
我改变了
ListView ls = (ListView) findViewById(R.id.list);
to ListView ls = (ListView) findViewById(android.R.id.list);
到
ListView ls = (ListView) findViewById(android.R.id.list);
and ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, events));
和
ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, events));
to ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, android.R.id.text1 , events));
到
ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, android.R.id.text1 , events));
and I get somehow a non-crash app but the behavior is weird. 而且我以某种方式获得了一个非崩溃的应用程序,但是行为很奇怪。
There is no need to define listview
again in Activity
when your Activity extends ListActivity
. 当您的Activity扩展
ListActivity
时,无需在Activity
再次定义listview
。 Therefore remove this line 因此删除此行
ListView ls = (ListView) findViewById(R.id.list);
And try to change 并尝试改变
ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, events));
to 至
setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1 ,events));
where simple_list_item_1
is Android internal layout view 其中
simple_list_item_1
是Android内部布局视图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.