如何在List中使ListView与ArrayAdapter一起使用

时间:2014-06-22 07:58:20

标签: android listview android-arrayadapter

我已经堆积了数小时试图解决这个问题。我将解释我的代码结构。

我有两个课程TwitterActivity(主要活动)和SearchTwitter。在TwitterActivity我登录然后开始SearchTwitter活动。我还有他们的xml布局文件,activity_twitter.xmlsearch.xml

这些课程如下:

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);
            }   
        }
    });
}

activity_twitter.xml只有一个按钮,登录按钮。 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
     });
}

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>

现在我的问题是什么

  • SearchTwitter活动中,我确实扩展了ListActivity

  • 我想做的就是让SearchTwitter中的星星包围的代码正常工作。我尝试了很多方法,但没有结果。我的应用程序总是崩溃。

我感到筋疲力尽,我不知道该怎么办。随意提出任何问题。任何形式的帮助,建议都非常感谢。感谢。

PS:星星包围的代码是错误的。

修改

在案件ListView ls = (ListView) findViewById(R.layout.search);我得到:

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)

编辑2

我将ListView ls = (ListView) findViewById(R.id.list);更改为ListView ls = (ListView) findViewById(android.R.id.list);并将ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, events));更改为ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, android.R.id.text1 , events));,我以某种方式获得了非崩溃应用,但行为很奇怪。

1 个答案:

答案 0 :(得分:0)

当您的活动扩展listview时,无需在Activity中再次定义ListActivity。 因此删除此行

ListView ls = (ListView) findViewById(R.id.list);

并尝试更改

ls.setAdapter(new ArrayAdapter<String> (this, R.layout.search, events));

setListAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1 ,events));

其中simple_list_item_1 Android内部布局视图