列表视图在android中的空指针异常

时间:2011-05-12 12:37:01

标签: android

大家好我有一个列表视图,在字符串数组中有一些值,我把它传递给列表视图

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

它在此行上获得NullPointerException。

我的代码:

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

            }


        }       
);

 }

}

我的xml文件:

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

我的Log cat文件:

 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)

3 个答案:

答案 0 :(得分:0)

我认为您应该使用R.layout.simple_list_item_1而不是android.R.layout.simple_list_item_1(并在构建之前清理项目)

答案 1 :(得分:0)

尝试让您的类扩展ListActivity而不是Activity

答案 2 :(得分:0)

id的{​​{1}}似乎不匹配:

XML布局中的id值可能与ListView中的id不同。清理您的项目并重建,看看是否能修复它。