我有一个布局,其中有一个列表视图。我对此列表视图的每个项目都有一个布局,名为: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