Android ListView.setAdapter()nullpointer异常

时间:2015-01-07 17:02:03

标签: android android-listview android-adapter

我得到了一个nullptr,我读了一些帖子,但我似乎无法追捕这个,我是应用程序开发的新手;任何帮助将不胜感激。错误发生在这里:

adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
            list.setAdapter(adapter);

以下是主要活动:

public class MainActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.ciltkick.nom.MainActivity";

    private EditText editTxt;
    private Button btn;
    private ListView list;
    private ArrayAdapter<String> adapter;
    private ArrayList<String> arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        editTxt = (EditText) findViewById(R.id.msg);
        btn = (Button) findViewById(R.id.addNom);
        list = (ListView) findViewById(R.id.list_item);
        arrayList = new ArrayList<String>();

        adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
        list.setAdapter(adapter);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

这是xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false">

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <EditText android:id="@+id/msg"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/msg"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/addNom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send"
            android:onClick="addNom"/>
    </TableRow>

    <TableRow
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical">

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

</TableLayout>

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ciltkick.nom" >
    <uses-sdk android:minSdkVersion="11"></uses-sdk>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondaryActivity"
            android:label="@string/title_activity_secondary"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.ciltkick.nom.MainActivity" />
        </activity>
    </application>

</manifest>

这是stacktrace:

01-07 17:52:04.494  26339-26339/com.example.ciltkick.nom E/Trace﹕ error opening trace file: No such file or directory (2)
01-07 17:52:06.455  26339-26339/com.example.ciltkick.nom E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ciltkick.nom/com.example.ciltkick.nom.MainActivity}: java.lang.ClassNotFoundException: com.example.ciltkick.nom.MainActivity
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1995)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: com.example.ciltkick.nom.MainActivity
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1986)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案