列表<mystructure>错误:应用程序崩溃,不知道为什么</mystructure>

时间:2013-08-10 13:30:41

标签: java android list class

我已经构建了一个名为“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);


    }

logcat的:

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

感谢你们的帮助!

5 个答案:

答案 0 :(得分:4)

初始化您的标记列表

List<item> markers=new ArrayList<item>();

答案 1 :(得分: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);


}

答案 2 :(得分: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);

答案 3 :(得分:0)

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

 List<item> markers=new ArrayList<item>();

答案 4 :(得分:0)

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

  1. 首先像这样

    将内存分配给Arraylist对象

    公共类MainActivity扩展了Activity {

    ArrayList markers;

    @覆盖   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);
    
  2. }

    1. 其次要处理错误,请使用try和catch块。
    2. 公共类MainActivity扩展了Activity {

      ArrayList markers;

      @覆盖       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);
      
          }
          catch(Exception e) 
          {
               //Write to android ddms logger.
                  or
                make a Toast Message
          }
      

      }

      1. 释放像arraylist等一样初始化的资源。  通过在catch块之后放置finally块。