-2

我已经建立了一些名为“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

感谢你们的帮助!

4

5 回答 5

4

初始化你的标记列表

List<item> markers=new ArrayList<item>();
于 2013-08-10T13:33:00.473 回答
1

试试这个

public class MainActivity extends Activity {

ArrayList<item> markers;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     markers = new ArrayList<item>();

    item a = new item(1, "lol", "sdfs", 32.45345, 34.54353, "nir", 0, 0);
    markers.add(a);


}
于 2013-08-10T13:36:39.397 回答
0

您必须在使用列表之前对其进行初始化。

    setContentView(R.layout.activity_main);

    List<item> markers = new ArrayList<item>();
    item a = new item(1, "lol", "sdfs", 32.45345, 34.54353, "nir", 0, 0);
    markers.add(a);
于 2013-08-10T13:33:47.750 回答
0

任何对象都应在使用其方法之前进行初始化,但是您似乎在向列表中添加数据之前忘记了这一步。markers在向其中添加数据之前进行初始化。

 List<item> markers=new ArrayList<item>();
于 2013-08-10T14:21:34.563 回答
0

在代码中,您尚未初始化 arraylist 对象以删除这些错误,请参阅下面提到的以下步骤:

  1. 首先像这样为 Arraylist 对象分配内存

    公共类 MainActivity 扩展 Activity {

    ArrayList 标记;

    @Override protected void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
    
           markers = new ArrayList<item>();
    
           item a = new item(1, "lol", "sdfs", 32.45345, 34.54353, "nir", 0, 0);
           markers.add(a);
    

}

  1. 其次,使用 try 和 catch 块来处理错误。

公共类 MainActivity 扩展 Activity {

ArrayList 标记;

@Override protected void onCreate(Bundle savedInstanceState) { try {

           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           markers = new ArrayList<item>();

           item a = new item(1, "lol", "sdfs", 32.45345, 34.54353, "nir", 0, 0);
           markers.add(a);

    }
    catch(Exception e) 
    {
         //Write to android ddms logger.
            or
          make a Toast Message
    }

}

  1. 释放已经初始化的资源,如arraylist等。通过将finally块放在catch块之后。
于 2013-08-11T11:23:31.187 回答