ListView适配器崩溃类

时间:2013-09-25 20:37:22

标签: android android-listview listview-adapter

我正在尝试为我的应用的新页面设置列表视图。现在我用一个名为x的虚拟数组填充它。每次我运行此活动时它都会崩溃,除非我注释掉list.setAdapter行。

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

2 个答案:

答案 0 :(得分:0)

列表永远不会在onCreate中声明。请务必致电

list=(ListView)findViewById(R.id.listId);
在onCreate中

。 CreateView将在之后调用。

答案 1 :(得分:0)

您可以在ListAdapter中创建但不设置onCreate(),因为此时您的视图尚未创建。在onCreateView()后调用onCreate()