0

我有一个布局,其中有一个列表视图。我对此列表视图的每个项目都有一个布局,名为:email_listview_item,这是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical" 
    android:layout_gravity="center_vertical"
    android:background="@color/antiquewhiter">

    <ImageView
        android:id="@+id/email_list_item_checkbox_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30dp"
        android:src="@drawable/form2andcheckbox_android" />

    <TextView
        android:id="@+id/email_list_item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/email_list_item_checkbox_icon"
        android:text="TextView" 
        android:textColor="@color/bisque"/>

    <ImageView
        android:id="@+id/email_listview_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="309dp"
        android:src="@drawable/apvpro_icon" />

</RelativeLayout>

我想用这些项目创建列表视图,但我得到了 nullPointer。你能帮我为什么吗?这是我的课的代码:

public class ListFragment extends ListActivity{
    ListView listView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        listView = (ListView) findViewById(R.id.email_listview);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
          "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
          "Linux", "OS/2" };

        // First paramenter - Context
        // Second parameter - Layout for the row
        // Third parameter - ID of the TextView to which the data is written
        // Forth - the Array of data
//      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//        R.layout.email_list_item, android.R.id.text1, values);
        Email_list_view_adapter adapter = new Email_list_view_adapter(this, values);
        // Assign adapter to ListView
        listView.setAdapter(adapter); 
    }




}

email_list_view_adapter:

public class Email_list_view_adapter extends ArrayAdapter<String>{
    private final Context context;
    private final String values[];

    public Email_list_view_adapter(Context context, String[] values){
        super(context, R.layout.email_list_item, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.email_list_item, parent,false);
        TextView desription = (TextView) rowView.findViewById(R.id.email_list_item_text);
        ImageView image = (ImageView) rowView.findViewById(R.id.email_listview_image);
        desription.setText(values[position]);
        String s = values[position];
        if(s.startsWith("iPhone")){
            image.setImageResource(R.drawable.senddata_android);
        }else{
            image.setImageResource(R.drawable.senddata_selected_android);
        }

        return rowView;
    }

}

这是堆栈跟踪:

08-20 14:27:03.334: E/AndroidRuntime(9196): FATAL EXCEPTION: main
08-20 14:27:03.334: E/AndroidRuntime(9196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spot.p2a/pdf2app.ListFragment}: java.lang.NullPointerException
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.ActivityThread.access$1500(ActivityThread.java:122)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.os.Looper.loop(Looper.java:132)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.ActivityThread.main(ActivityThread.java:4025)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at java.lang.reflect.Method.invokeNative(Native Method)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at java.lang.reflect.Method.invoke(Method.java:491)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at dalvik.system.NativeStart.main(Native Method)
08-20 14:27:03.334: E/AndroidRuntime(9196): Caused by: java.lang.NullPointerException
08-20 14:27:03.334: E/AndroidRuntime(9196):     at pdf2app.ListFragment.onCreate(ListFragment.java:30)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-20 14:27:03.334: E/AndroidRuntime(9196):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
08-20 14:27:03.334: E/AndroidRuntime(9196):     ... 11 more
4

4 回答 4

3

您没有定义活动的布局

 setContentView(R.layout.your_layout);
于 2012-08-20T12:44:54.060 回答
2

在从 xml 分配 ListView 引用之前,您应该指定 setContentView() 与 ListView 持有的布局。这里缺少 NullPointer 异常的问题。setContentView(R.layout.my_list)最好在 super.onCreate(savedInstanceState); 旁边立即调用一个方法;

于 2012-08-20T12:46:12.890 回答
2

由于您的活动是ListActivity

第一次打电话,

setContentView(R.layout.listview_layout);

现在你可以

listView = getListView();

代替

listView = (ListView) findViewById(R.id.email_listview);

看看这个关于ListView 和 ListActivity的完整教程

于 2012-08-20T12:51:14.690 回答
1

您将在此行获得 NullPointerException:

        listView = (ListView) findViewById(R.id.email_listview);

这是因为您还没有分配一个布局来查找该 View,因此 Android 在每个 Activity 和 Fragment 具有的默认 FrameLayout 中查找它。你有两个解决方案:

setContentView(R.layout.your_layout_file);1)使用前打电话findViewById()

2)在该行之前添加View v = getLayoutInflater().inflate(R.layout.button, null);,然后将该行更改为listView = (ListView) v.findViewById(R.id.email_listview);

于 2012-08-20T12:48:55.767 回答