0

我正在尝试为我的应用程序的新页面设置一个列表视图。现在我让它被一个名为 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
4

2 回答 2

0

列表从未在 onCreate 中声明。一定要打电话

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

在创建。之后将调用 CreateView。

于 2013-09-25T20:41:49.013 回答
0

您可以创建但不能设置ListAdapteronCreate()因为此时尚未创建您的视图。onCreateView()之后被调用onCreate()

于 2013-09-25T20:46:49.543 回答