小编Joh*_*ohn的帖子

当我引用ArrayList.add方法时,Android应用程序在启动时崩溃

这是我的MainActivity的完整代码.如你所见,我只添加了2行:

ArrayList<String> y = null; 
y.add(new String("buggy")); // When I remove this line it works
Run Code Online (Sandbox Code Playgroud)

当我注释掉y.add()时,应用程序不会崩溃.

我的问题是:为什么使用y.add()导致程序失败?

移动设备是三星Galaxy Mini S5570,运行Android 2.3.4

这是完整的代码,后面是LogCat:

package com.testbug;
import javax.*;
import java.util.*;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu; 
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //
    // Only these 2 lines added by me. The rest is automatically generated in Eclipse.
    ArrayList<String> y = null; 
    y.add(new String("buggy")); // When I remove this line …
Run Code Online (Sandbox Code Playgroud)

java crash android startup nullpointerexception

2
推荐指数
1
解决办法
2483
查看次数

标签 统计

android ×1

crash ×1

java ×1

nullpointerexception ×1

startup ×1