2013-04-22 169 views
-2

我不知道爲什麼我一次又一次地得到這個錯誤.. 任何幫助將不勝感激..我檢查過清單.xml..no問題是ther..plz檢查,並提供可能的話java.lang.RuntimeException:無法實例化活動:Java.lang.nullpointerException

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);              
     } 
    } 
    } 

} 

這裏是日誌貓

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);              
     } 
    } 
    } 
+0

發表您的logcat – DigCamara 2013-04-22 15:33:21

+0

請粘貼您的logcat報告以檢測它崩潰 – 2013-04-22 15:34:29

+0

您需要發佈您的logcat(類型:從終端窗口亞行logcat) – Booger 2013-04-22 15:35:11

回答

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)); 
+0

String num =((TextView)view).getText()。toString(); 我已經添加了這行代替 TextView textitem =(TextView)findViewById(R.id.new_list); 和移動建議藥水在oncreatemethod ..它仍然給錯誤。 – user2169509 2013-04-22 16:19:42

+0

看看Pragnani的答案。他添加了一段我錯過的代碼。 – DigCamara 2013-04-22 16:24:15

相關問題