【发布时间】:2015-02-06 15:27:28
【问题描述】:
这是发生错误的主要活动
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.Events);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<>(this, R.layout.list_data, R.id.label, adobe_products));
ListView lv = getListView();
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// In the following line "v" refers to the View returned by the `getView()` method; meaning the clicked View.
TextView txtName = (TextView) v.findViewById(R.id.product_label);
String name;
name = txtName.getText().toString();
switch (name) {
case "Sports":
Intent intent;
intent = new Intent(MainActivity.this, Sports.class);
startActivity(intent);
break;
case "Lunch":
Intent intent1;
intent1 = new Intent(MainActivity.this, Lunch.class);
startActivity(intent1);
break;
case "contacts":
Intent intent2;
intent2 = new Intent(MainActivity.this, Contacts.class);
startActivity(intent2);
break;
}
}
});
}
}
这是错误
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.CharSequence android.widget.TextView.getText()” 在 com.example.trobinson.fblaproject.MainActivity$1.onItemClick(MainActivity.java:38) 在 android.widget.AdapterView.performItemClick(AdapterView.java:300) 在 android.widget.AbsListView.performItemClick(AbsListView.java:1143) 在 android.widget.AbsListView$PerformClick.run(AbsListView.java:3044) 在 android.widget.AbsListView$3.run(AbsListView.java:3833) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5221) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 02-06 09:52:44.609 938-962/? E/gralloc_goldfish:gralloc_alloc:不匹配的使用标志:576 x 576,使用333 02-06 09:52:44.609 938-962/? E/: GraphicBufferAlloc::createGraphicBuffer(w=576, h=576) failed (Invalid argument), handle=0x0 02-06 09:52:44.624 1249-1280/system_process E/BufferQueueProducer: [ScreenshotClient] dequeueBuffer: createGraphicBuffer 失败 02-06 09:52:44.629 1249-1280/system_process E/ActivityManager: 缩略图尺寸无效:576x576 02-06 10:12:01.500 2233-2233/? E/memtrack:无法加载memtrack模块(没有这样的文件或目录) 02-06 10:12:01.500 2233-2233/? E/android.os.Debug:加载memtrack模块失败:-2 02-06 10:12:01.963 2243-2243/? E/libprocessgroup﹕无法制作和 chown /acct/uid_10054: 只读文件系统 02-06 10:12:08.952 2243-2243/com.example.trobinson.fblaproject E/AndroidRuntime: 致命例外:主要 进程:com.example.trobinson.fblaproject,PID:2243
我们不知道该怎么办。
【问题讨论】:
-
请提供一个体面的代码示例,说明异常发生的位置和完整的错误描述。没有它,我们很难确定哪里出了问题
-
检查你的标签,标题说 Android Studio 你的标签说 eclipse
-
java.lang.NullPointerException
-
好的,我们将如何解决@paul?
-
很有可能第 38 行的内容从未被初始化。 38 是哪一行?
标签: android main-activity