【发布时间】:2016-05-16 10:03:48
【问题描述】:
我目前正在开发我的第一个 Android 应用程序。在应用程序中应该有一个 listView,它在左侧显示一张图片,旁边显示 2 个 textView。
listView 的 xml 如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
MainActivity 的 xml 看起来是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView xmlns:android="rowlayout"
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
我的 MainActivity.java 代码如下所示:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
String[] values2 = new String[] {"good","bad","medium","not yet tested","don't know it","very cool","Just Windows","piece of shit","very cool","don't know it too"};
ListView listView = (ListView) findViewById(R.id.listview);
ListViewAdapter adapter1 = new ListViewAdapter (values,values2,android.R.id.list);
listView.setAdapter(adapter1);
}
}
我当前的自定义适配器如下所示:
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter {
String[] values;
String[] values2;
Context ctxt;
LayoutInflater layoutInflater;
public ListViewAdapter (String [] Values,String [] Values2, Context context) {
values = Values;
values2 = Values2;
ctxt = context;
layoutInflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return values.length;
}
@Override
public Object getItem(int position) {
String [] res = new String [] {values [position],values2 [position]};
return res;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView name1 = (TextView)convertView.findViewById(android.R.id.text1);
TextView name2 = (TextView)convertView.findViewById(android.R.id.text2);
name1.setText(values [position]);
name2.setText(values2 [position]);
return convertView;
}
}
启动应用时出现以下错误:
进程:de.gotthold.myapplication2.app,PID:16723 java.lang.RuntimeException:无法启动活动 ComponentInfo{de.gotthold.myapplication2.app/de.gotthold.myapplication2.app.MainActivity}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 空对象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 在 android.app.ActivityThread.access$800(ActivityThread.java:144) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:155) 在 android.app.ActivityThread.main(ActivityThread.java:5727) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 引起:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 空对象引用 在 de.gotthold.myapplication2.app.MainActivity.onCreate(MainActivity.java:19) 在 android.app.Activity.performCreate(Activity.java:5961) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 在 android.app.ActivityThread.access$800(ActivityThread.java:144) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:155) 在 android.app.ActivityThread.main(ActivityThread.java:5727) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 02-06 12:45:27.641 16723-16723/? D/Process﹕killProcess, pid=16723 02-06 12:45:27.641 16723-16723/? D/进程:com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
我希望获得有关此问题的解释,而不是指向教程的链接,因为我尝试通过它们进行工作并且发生了这种情况。我还阅读了多个 stackoverflow 线程,但没有一个可以解决我的问题。
感谢您的帮助, 小学生
P.S.:这不是我正在开发的应用程序,这是一个不同的应用程序,我正在用我学到的新知识搞乱。
【问题讨论】: