ListView崩溃致命异常

时间:2015-04-24 10:49:33

标签: java android listview arraylist

我有ListView来显示ArrayList的内容。我从我的方法中调用listView.setAdapter

在我的应用程序中,我可以从main_actiivty中获取topten_activity,或者如果我结束游戏,我得到并且意图引用我向(Person)添加数据; 我的Topten_activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_1"
android:orientation="vertical" >

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

每次启动活动/意图时。应用程序崩溃

FATAL EXCEPRION:main.

我在设置适配器的方法中使用了Log.d。日志被打印,所以呼叫到达那里

请查看下面的代码

代码:

public class TopTen extends ListActivity{

Person p;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_topten);
    Log.d("inside", "TopTen OnCreat");
    populateListView();
}

private void populateListView(){
    Log.d("inside", "populateListView");
    ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, R.layout.activity_topten,p.TopTen);
    ListView list =(ListView) findViewById(android.R.id.list);
    list.setAdapter(adapter);
}   
}

堆栈跟踪:

 FATAL EXCEPTION: main
 Process: com.example.flagsgame, PID: 29436
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flagsgame/com.example.flagsgame.TopTen}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5146)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at com.example.flagsgame.TopTen.populateListView(TopTen.java:26)
    at com.example.flagsgame.TopTen.onCreate(TopTen.java:21)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)

Person.class:

public class Person extends Activity{

EditText name;
Button ok;
TextView enter;
Integer Score;
String Name;
Person person;
Person a;
Person b;
Person c;

//Map<String,Integer>Names=new HashMap<String,Integer>(10); 
ArrayList<Person>TopTen = new ArrayList<Person>(10);
//Map sortedMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_person);

    Intent intent = getIntent();
    Score = intent.getIntExtra("Score", 0);
    name =(EditText)findViewById(R.id.namep);
    ok =(Button)findViewById(R.id.send);
    enter =(TextView)findViewById(R.id.entername);
    Toast toast = Toast.makeText(getApplicationContext(), "Lets see if you are can be on TopTen", Toast.LENGTH_SHORT);

    a.Name="Daniel";
    a.Score=20;
    b.Name="Zohar";
    b.Score=15;
    c.Name="Yakir";
    c.Score=10;

    TopTen.add(a);
    TopTen.add(b);
    TopTen.add(c);

    ok.setOnClickListener(SendActionListener);
}

private OnClickListener SendActionListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Person lastEntry = TopTen.get(TopTen.size()-1);//Last element of the arraylist
        if(lastEntry.Score < Score){//checking score
            lastEntry.Score = Score;//put score aka Value
            lastEntry.Name=name.getText().toString();//put name aka Key
            Toast toast = Toast.makeText(getApplicationContext(), "You are now on the TopTen List", Toast.LENGTH_SHORT);
        }
    };

};

}//end of Person

3 个答案:

答案 0 :(得分:0)

Pivot尚未初始化

答案 1 :(得分:0)

您的代码中存在一个问题。也就是说,您从一个活动到另一个活动获得ArrayList值。还有一点你在评论中提到,没有两个活动被称为只有一个叫做。在这种情况下,你初始化与否并不重要,你可能会得到空指针。

所以和平解决方案只是在自己的Activity中初始化ArrayList。

例如:

在TopTen活动初始化ArrayList

ArrayList<Person>TopTen_array = new ArrayList<Person>(10);

在onCreate

中向ArrayList添加值
 a.Name="Daniel";
        a.Score=20;
        b.Name="Zohar";
        b.Score=15;
        c.Name="Yakir";
        c.Score=10;

        TopTen_array.add(a);
        TopTen_array.add(b);
        TopTen_array.add(c);

并替换以下行

ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, R.layout.activity_topten,p.TopTen);

用这个

ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(this, R.layout.activity_topten,TopTen_array);

答案 2 :(得分:0)

您尚未在Person方法上初始化onCreate()课程,这是p.TopTen为空的原因。所以你需要通过

来做
 p = new Person();