2013-09-25 104 views
0

我試圖爲我的應用程序的新頁面設置一個列表視圖。現在我有一個名爲x的虛擬數組填充它。每次運行此活動時,都會崩潰,除非我註釋掉list.setAdapter行。ListView適配器崩潰類

public class addpage extends Activity{ 

private TextView text; 
private ListView list; 



final ArrayList<HashMap<String,String>> x = new ArrayList<HashMap<String,String>>(); 




public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.nearplace, container, false); 
    Log.w("TAB","NEWS"); 
    if (container == null) { 
     return null; 
    } 

    setTitle("News"); 

    text = (TextView) v.findViewById(R.id.nomore); 
    list = (ListView) v.findViewById(R.id.newslist); 
    list.setDividerHeight(2); 

    //populateData(); 

    return v; 
} 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    overridePendingTransition(R.layout.top_out,R.layout.top_in); 
    setTheme(R.style.Theme_Dolocal_theme); 
    setContentView(R.layout.nearplace); 

    super.onCreate(savedInstanceState); 



    HashMap<String,String> temp = new HashMap<String,String>(); 

    temp.put("title", "Test"); 
    temp.put("content", "Content"); 

     x.add(temp); 


    final ListAdapter adapter = new SimpleAdapter(this, x, R.layout.news_listview, 
      new String[] {"title","content"}, 
      new int[] {R.id.title, R.id.content}); 

    list.setAdapter(adapter); 



} 

}

這是我的控制檯

09-25 20:18:40.126: E/AndroidRuntime(12013): FATAL EXCEPTION: main 
09-25 20:18:40.126: E/AndroidRuntime(12013): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.testapp/com.testapp.testapp.addpage}: java.lang.NullPointerException 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.os.Looper.loop(Looper.java:137) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.ActivityThread.main(ActivityThread.java:5041) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at java.lang.reflect.Method.invokeNative(Native Method) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at java.lang.reflect.Method.invoke(Method.java:511) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at dalvik.system.NativeStart.main(Native Method) 
09-25 20:18:40.126: E/AndroidRuntime(12013): Caused by: java.lang.NullPointerException 
09-25 20:18:40.126: E/AndroidRuntime(12013): at com.testapp.testapp.addpage.onCreate(addpage.java:88) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.Activity.performCreate(Activity.java:5104) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
09-25 20:18:40.126: E/AndroidRuntime(12013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
09-25 20:18:40.126: E/AndroidRuntime(12013): ... 11 more 

回答

0

列表永遠不會在聲明的onCreate。請務必致電

list=(ListView)findViewById(R.id.listId); 

in onCreate。 CreateView將在以後調用。

0

您可以在onCreate()中創建但不設置ListAdapter,因爲您的視圖目前尚未創建。 onCreateView()onCreate()後被調用。