使用ArrayAdapter或ListView崩溃

时间:2017-09-23 11:24:20

标签: java android listview android-arrayadapter

我刚刚使用ArrayAdapyer和ListView测试, 当我运行此代码时,我的应用程序崩溃。日志什么也没说,发现没有错误。 这是班级:

和班级:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView list;
    ArrayAdapter<String> adap;
    ArrayList<String> arr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = (ListView) findViewById(R.id.list);
        arr = new ArrayList<String>();
        arr.add("hello");
        arr.add("name is dragon");
        adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
        list.setAdapter(adap);


    }

}

LOG:

                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.faivel.myapplication/com.example.faivel.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
                                               at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                               at android.os.Looper.loop(Looper.java:154)
                                               at android.app.ActivityThread.main(ActivityThread.java:6776)
                                               at java.lang.reflect.Method.invoke(Native Method)
                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
                                               at com.example.faivel.myapplication.MainActivity.onCreate(MainActivity.java:26)
                                               at android.app.Activity.performCreate(Activity.java:6955)
                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
                                               at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                               at android.os.Looper.loop(Looper.java:154) 
                                               at android.app.ActivityThread.main(ActivityThread.java:6776) 
                                               at java.lang.reflect.Method.invoke(Native Method) 
                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 

这里有什么问题?谢谢! 编辑:添加日志!

1 个答案:

答案 0 :(得分:0)

我测试了您的class

package com.example.nniroula.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class Test extends AppCompatActivity {

    ListView list;
    ArrayAdapter<String> adap;
    ArrayList<String> arr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        list = (ListView) findViewById(R.id.list);
        arr = new ArrayList<String>();
        arr.add("hello");
        arr.add("name is dragon");
        adap = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arr);
        list.setAdapter(adap);


    }

}

xml文件

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

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

</LinearLayout>

一切正常。