我的应用程序无法启动。它在打开时崩溃

时间:2019-11-05 15:24:15

标签: java android

andriod无法启动应用程序

试图清理项目不起作用。打开前应用崩溃。

 @Override 
 protected void onCreate(Bundle savedInstanceState) {
    List<String> list = new ArrayList<String>();
    ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

    list.add("Hallo");
    list.add("Nikola");
    list.add("Lamar");

    ListView view = (ListView) findViewById(R.id.myView);
    view.setAdapter(itemsAdapter);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/myView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

1 个答案:

答案 0 :(得分:2)

设置内容视图后,您应尝试访问您的视图。在那之前,您的视图不存在于层次结构中。您可以尝试这样的事情:

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<String> list = new ArrayList<String>();
    ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

    list.add("Hallo");
    list.add("Nikola");
    list.add("Lamar");

    ListView view = (ListView) findViewById(R.id.myView);
    view.setAdapter(itemsAdapter);
}