我已经建立了一些名为“item”的类,这是它:(这就是整个代码)
public class item {
private int id;
private String title;
private String desc;
private double lat;
private double lon;
private String pub;
private int p;
private int n;
public item(int id, String title, String desc, double lat, double lon, String pub, int p, int n) {
super();
this.id = id;
this.title = title;
this.desc = desc;
this.lat = lat;
this.lon = lon;
this.pub = pub;
this.p = p;
this.n = n;
}
现在我必须为它制作一个“项目” List<item>
,add()
但由于某种原因,我的应用程序崩溃了。
那就是活动:
public class MainActivity extends Activity {
List<item> markers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
item a = new item(1, "lol", "sdfs", 32.45345, 34.54353, "nir", 0, 0);
markers.add(a);
}
日志猫:
08-10 16:32:46.710: D/AndroidRuntime(2934): Shutting down VM
08-10 16:32:46.710: W/dalvikvm(2934): threadid=1: thread exiting with uncaught exception (group=0x41a0a930)
08-10 16:32:46.777: E/AndroidRuntime(2934): FATAL EXCEPTION: main
08-10 16:32:46.777: E/AndroidRuntime(2934): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testitems/com.example.testitems.MainActivity}: java.lang.NullPointerException
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.ActivityThread.access$600(ActivityThread.java:153)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.os.Looper.loop(Looper.java:137)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.ActivityThread.main(ActivityThread.java:5227)
08-10 16:32:46.777: E/AndroidRuntime(2934): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 16:32:46.777: E/AndroidRuntime(2934): at java.lang.reflect.Method.invoke(Method.java:511)
08-10 16:32:46.777: E/AndroidRuntime(2934): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-10 16:32:46.777: E/AndroidRuntime(2934): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-10 16:32:46.777: E/AndroidRuntime(2934): at dalvik.system.NativeStart.main(Native Method)
08-10 16:32:46.777: E/AndroidRuntime(2934): Caused by: java.lang.NullPointerException
08-10 16:32:46.777: E/AndroidRuntime(2934): at com.example.testitems.MainActivity.onCreate(MainActivity.java:18)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.Activity.performCreate(Activity.java:5104)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-10 16:32:46.777: E/AndroidRuntime(2934): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-10 16:32:46.777: E/AndroidRuntime(2934): ... 11 more
感谢你们的帮助!