开发者

list view null pointer exception in android

hi all i have a list view with some values in string array and i pass it to list view by

 list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array)); 

it get NullPointerException on this line.

my code:

public class SecondActivity extends Activity{

int min=00,hr=00,sec=00;
TextView tv1;
ListView list1;
private String array[] = { "Iphone", "Tutorials", "Gallery", "Android","item 1", "item 2", "item3", "item 4" }; 

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondactivity);

    tv1=(TextView)findViewById(R.id.textView1);
    list1 = (ListView) findViewById(R.id.list);

    Bundle bundle = this.getIntent().getExtras();
    hr = Integer.parseInt(bundle.getString("key1"));
    min = Integer.parseInt(bundle.getString("key2"));
    sec=Integer.parseInt(bundle.getString("key3"));

    tv1.setText(String.format("%02d:%02d:%02d", hr, min, sec));


    list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, array));      // line no 38  


list1.setOnItemClickListener(
        new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View view,int position, long id) {
                // TODO Auto-generated method stub
                Object o = list1.getItemAtPosition(position);
                String pen = o.toString();
                Toast.makeText(getApplicationContext(), "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();


         开发者_如何学运维   }

            public void onItemClick1(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {   // eclipse told me to add Unimplemented method(this method).
                // TODO Auto-generated method stub

            }


        }       
);

 }

}

my xml file:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
   <TextView android:text="TextView" android:id="@+id/textView1"     android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

  <ListView android:id="@+id/list"  android:layout_width="fill_parent"   android:layout_height="wrap_content" android:layout_marginLeft="10px" android:layout_marginRight="10px" android:layout_marginTop="65px" android:layout_marginBottom="40px" android:paddingLeft="0px"
android:paddingRight="0px" />

</LinearLayout>

my Log cat file:

 05-12 17:50:12.295: ERROR/AndroidRuntime(3776): Caused by: java.lang.NullPointerException
  05-12 17:50:12.295: ERROR/AndroidRuntime(3776):     at com.stop.watch.SecondActivity.onCreate(SecondActivity.java:38)
  05-12 17:50:12.295: ERROR/AndroidRuntime(3776):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 05-12 17:50:12.295: ERROR/AndroidRuntime(3776):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)


It looks like there is a mismatch between the id for your ListView:

The id value in your XML layout might be different from the id that comes from the Bundle. Clean your project and rebuild, and see if that fixes it.


I think you should use R.layout.simple_list_item_1 and not android.R.layout.simple_list_item_1 (and clean the project before building it)


Try having your class extend ListActivity rather than Activity

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜