简体   繁体   中英

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 . In TwitterActivity I sign in and then start the SearchTwitter activity. I have also their xml layout files, activity_twitter.xml and 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. The SearchTwitter activity looks like:

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:

<?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

  • All I want to do is to make the code surrounded by stars in SearchTwitter work. 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.

EDIT

In the case 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

I changed ListView ls = (ListView) findViewById(R.id.list); to ListView ls = (ListView) findViewById(android.R.id.list); and 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)); 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 . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM