Android 无法启动 Activity listview setadapter 出现空指针异常

Android unable to start Activity null pointer exception at listview setadapter

这个 activity 是功能的副本。我找不到我的错误。我也进行了广泛的搜索但没有成功。这是我的第一个应用程序,我们将不胜感激任何帮助。错误发生在列表视图集适配器 主要 activity

     package com.example.jaclen.queensdreambook;

     import android.app.Activity;
     import android.os.Bundle;
     import android.widget.ArrayAdapter;
     import android.widget.ListView;



    public class bMainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b_main);
    ListView listViewbwlo = (ListView) findViewById(R.id.ListViewbwlo);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(

                this, R.array.bw, android.R.layout.simple_list_item_1);

          listViewbwlo.setAdapter(adapter);

    }

}

布局xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#DAA520">




      <ListView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/listViewbwlo"
          android:divider="#000000"
          android:dividerHeight="@dimen/activity_vertical_margin">



          </ListView>


</LinearLayout>

清单 android:versionCode="1" android:versionName="1.0" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".QueenActivityMainActivity"
            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=".ainfoMainActivity"
            android:label="Queens Dream Book"
            android:parentActivityName=".QueenActivityMainActivity" >

           <intent-filter>
                <action android:name="com.example.jaclen.queensdreambook.ainfoMainActivity" />
                <category android:name="android.intent.category.DEFAULT" />
           </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.jaclen.queensdreambook.QueenActivityMainActivity" />

        </activity>


        <activity
            android:name=".bMainActivity"
            android:label="@string/QueensDreamBook"
            android:parentActivityName=".QueenActivityMainActivity" >
            <intent-filter>
                <action android:name="com.example.jaclen.queensdreambook.bMainActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.jaclen.queensdreambook.QueenActivityMainActivity" />
        </activity>

Logcat

03-24 15:21:03.080  12337-12337/com.example.jaclen.queensdreambook I/System.out﹕ Sending WAIT chunk
03-24 15:21:03.280  12337-12337/com.example.jaclen.queensdreambook I/System.out﹕ Debugger has connected
03-24 15:21:03.280  12337-12337/com.example.jaclen.queensdreambook I/System.out﹕ Debugger has connected
1:03.480  12337-12337/com.example.jaclen.queensdreambook I/System.out﹕ `enter code here`waiting for debugger to settle...
1:03.690  12337-12337/com.example.jaclen.queensdreambook I/System.out﹕ waiting for debugger to settle...
03-24 15:21:04.892  12337-12337/com.example.jaclen.queensdreambook I/System.out﹕ debugger has settled (1441)
4 15:21:06.073  12337-12337/com.example.jaclen.queensdreambook I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888.
12337/com.example.jaclen.queensdreambook D/memalloc﹕ ion: Mapped buffer base:0x5bd9c000 size:1536000 offset:0 fd:58
03-24 15:21:06.103  12337-12337/com.example.jaclen.queensdreambook E/﹕ Can't open file for reading
03-24 15:21:06.113  12337-12337/com.example.jaclen.queensdreambook E/﹕ Can't open file for reading
03-24 15:21:06.964  12337-12337/com.example.jaclen.queensdreambook D/memalloc﹕ ion: Mapped buffer base:0x5c28d000 size:1536000 offset:0 fd:62
03-24 15:21:07.014  12337-12337/com.example.jaclen.queensdreambook D/memalloc﹕ ion: Mapped buffer base:0x5c414000 size:1536000 offset:0 fd:65
03-24 15:28:27.544  12337-12337/com.example.jaclen.queensdreambook W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40d01210)
03-24 15:28:27.564  12337-12337/com.example.jaclen.queensdreambook E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jaclen.queensdreambook/com.example.jaclen.queensdreambook.bMainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access0(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4428)
            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:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.jaclen.queensdreambook.bMainActivity.onCreate(bMainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:4465)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
            at android.app.ActivityThread.access0(ActivityThread.java:123)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4428)
            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:787)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
            at dalvik.system.NativeStart.main(Native Method)

试试这个。

更改此部分

 ListView listViewbwlo = (ListView) findViewById(R.id.ListViewbwlo);

到此

 ListView listViewbwlo = (ListView) findViewById(R.id.listViewbwlo);

看来你拿错了id。

试试这个

在您的 XML 文件中,您使用 listview 并像这样提供 id

android: id="@+id/listViewbwlo"

在您的 Activitie 文件中,当您使用 findviewById() 获取 listview 时,您给出的 id 错误,因为 java 是一种区分大小写的语言,因此正确的语句如

ListView listViewbwlo=(ListView)findviewById(R.id.listViewbwlo);

这样你就不会在编译时出错,否则你会在编译时出错。因为当时id不喜欢所以报错