java.lang.RuntimeException:无法实例化活动:Java.lang.nullpointerException

时间:2013-04-22 15:31:13

标签: android

我不知道为什么我一次又一次地收到这个错误.. 任何帮助将非常感谢..我已经检查了manifest.xml ..没有问题是ther..plz检查并提供解决方案,如果可能的话

    package com.example.helloworld;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.TextView;

      public class List extends ListActivity {

      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);


      String[] numbers = {"one","two","three","four"};


      this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.new_list,numbers));

      ListView lv = getListView();


      lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {

      TextView textitem =(TextView)findViewById(R.id.new_list);

      String num = ((TextView) textitem).getText().toString();


      Intent i = new Intent("com.example.helloworld.CHECKBOX");

      i.putExtra("number", num);
      startActivity(i);

  }
});
}
}

这是manifest.xml ..

      <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloworld.Splash"
            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="com.example.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld.MAINACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.List"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld.LIST" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld.MENU" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.CheckPermissions"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld.CHECKPERMISSIONS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.TextPlay"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld.TEXTPLAY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.CheckboxCursorAdapter"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.helloworld.CHECKBOXCURSORADAPTER" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.helloworld.CheckBox"
            android:label="@string/title_activity_check_box" >
            <intent-filter>
                <action android:name="com.example.helloworld.CHECKBOX" />

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

</manifest>

最后这是checkbox.java

        package com.example.helloworld;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.ListActivity;
import android.util.SparseBooleanArray;
import android.widget.ArrayAdapter;


public class CheckBox extends ListActivity {

    String classes[] = { "MainActivity","CheckPermissions","TextPlay","example3","example4","example5","example6"};
    //ListView lView ;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, classes));


}
    List<Integer> checkedIDs = new ArrayList<Integer>();                            
    // Get all of the items that have been clicked - either on or off
    final SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
    {
    for (int i = 0; i < checkedItems.size(); i++){
        // And this tells us the item status at the above position
        final boolean isChecked = checkedItems.valueAt(i);
        if (isChecked){
            // This tells us the item position we are looking at
            final int position = checkedItems.keyAt(i);                                             
            // Put the value of the id in our list
            checkedIDs.add(position);                                                       
        }
    }
    }

}

这是log cat

04-22 15:14:00.236: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:00.616: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:00.656: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.336: D/dalvikvm(1615): GC_FOR_ALLOC freed 71K, 7% free 2515K/2704K, paused 54ms, total 58ms
04-22 15:14:01.596: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.596: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.618: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.636: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.748: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.756: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.768: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.857: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.857: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.866: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.866: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.866: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.866: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:01.896: D/gralloc_goldfish(1615): Emulator without GPU emulation detected.
04-22 15:14:02.356: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:02.426: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:04.646: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:04.796: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:04.796: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:05.136: D/dalvikvm(1615): GC_CONCURRENT freed 15K, 5% free 2903K/3056K, paused 6ms+135ms, total 322ms
04-22 15:14:05.556: D/dalvikvm(1615): GC_FOR_ALLOC freed 8K, 5% free 2975K/3112K, paused 91ms, total 98ms
04-22 15:14:05.609: I/dalvikvm-heap(1615): Grow heap (frag case) to 3.624MB for 635812-byte allocation
04-22 15:14:05.766: D/dalvikvm(1615): GC_FOR_ALLOC freed <1K, 4% free 3596K/3736K, paused 156ms, total 156ms
04-22 15:14:06.046: D/dalvikvm(1615): GC_CONCURRENT freed 1K, 4% free 3610K/3736K, paused 11ms+110ms, total 282ms
04-22 15:14:06.206: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.236: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.336: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.366: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.476: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.618: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.626: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.646: I/Choreographer(1615): Skipped 81 frames!  The application may be doing too much work on its main thread.
04-22 15:14:06.656: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.666: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.876: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:06.956: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.006: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.077: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.147: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.147: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.206: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.206: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.526: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.598: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.598: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.666: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.676: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.676: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:07.977: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.306: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.306: I/Choreographer(1615): Skipped 75 frames!  The application may be doing too much work on its main thread.
04-22 15:14:08.306: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.306: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.396: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.407: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.436: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.436: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.496: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:08.496: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:09.929: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:09.937: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.021: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.086: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.086: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.097: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.207: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.207: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.286: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.296: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.588: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.588: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.616: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.686: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.686: W/Trace(1615): Unexpected value from nativeGetEnabledTags: 0
04-22 15:14:10.696: D/AndroidRuntime(1615): Shutting down VM
04-22 15:14:10.696: W/dalvikvm(1615): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
04-22 15:14:10.846: E/AndroidRuntime(1615): FATAL EXCEPTION: main
04-22 15:14:10.846: E/AndroidRuntime(1615): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld/com.example.helloworld.CheckBox}: java.lang.NullPointerException
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.os.Looper.loop(Looper.java:137)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ActivityThread.main(ActivityThread.java:5039)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at java.lang.reflect.Method.invoke(Method.java:511)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at dalvik.system.NativeStart.main(Native Method)
04-22 15:14:10.846: E/AndroidRuntime(1615): Caused by: java.lang.NullPointerException
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.Activity.setContentView(Activity.java:1881)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ListActivity.ensureList(ListActivity.java:312)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ListActivity.getListView(ListActivity.java:297)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at com.example.helloworld.CheckBox.<init>(CheckBox.java:26)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at java.lang.Class.newInstanceImpl(Native Method)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at java.lang.Class.newInstance(Class.java:1319)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
04-22 15:14:10.846: E/AndroidRuntime(1615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
04-22 15:14:10.846: E/AndroidRuntime(1615):     ... 11 more
04-22 15:14:14.096: I/Process(1615): Sending signal. PID: 1615 SIG: 9

更新的代码: list.java

        lv.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent, View view,
      int position, long id) {


      // selected item 
      //String num = ((TextView) view).getText().toString();

      // Launching new Activity on selecting single List Item
      Intent i = new Intent("com.example.helloworld.CHECKBOX");
      // sending data to new activity
      //i.putExtra("number", num);
      startActivity(i);

  }

CheckBox.java

 this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, classes));
    List<Integer> checkedIDs = new ArrayList<Integer>();                            
    // Get all of the items that have been clicked - either on or off
    final SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
    {
    for (int i = 0; i < checkedItems.size(); i++){
        // And this tells us the item status at the above position
        final boolean isChecked = checkedItems.valueAt(i);
        if (isChecked){
            // This tells us the item position we are looking at
            final int position = checkedItems.keyAt(i);                                             
            // Put the value of the id in our list
            checkedIDs.add(position);                                                       
        }
    }
    }

1 个答案:

答案 0 :(得分:0)

移动

final SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
{
    for (int i = 0; i < checkedItems.size(); i++){
        // And this tells us the item status at the above position
        final boolean isChecked = checkedItems.valueAt(i);
        if (isChecked){
            // This tells us the item position we are looking at
            final int position = checkedItems.keyAt(i);                                             
            // Put the value of the id in our list
            checkedIDs.add(position);                                                       
        }
    }
}

后立即进入你的onCreate()方法
 this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, classes));