为什么我的应用程序崩溃?抽屉

时间:2015-05-12 11:41:52

标签: java android

我有问题,我的应用程序甚至没有启动,它只是崩溃。 我非常感谢你的帮助。 :) 我已经尝试使用listView = (ListView) findViewById(R.id.drawerList);

但是抽屉里没有元素。

MainActivity.java:

package com.CE.DE;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;
@Override
protected void onCreate(Bundle savedInstanceState){


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
    planets=getResources().getStringArray(R.array.planets);

     listView.setAdapter(new ArrayAdapter<String>                         
    (this,android.R.layout.simple_list_item_1,planets));
    }
 }

activity_main.xml中

<android.support.v4.widget.DrawerLayout   
xmlns:android="http://schemas.android.com/apk/res/android"

  android:id="@+id/drawerLayout"
   android:layout_width="match_parent"
  android:layout_height="match_parent">

   <FrameLayout

    android:id="@+id/mainContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    </FrameLayout>

   <ListView
    android:id="@+id/drawerList"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left">
    </ListView>
    </android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:2)

试用此代码

 package com.CE.DE;


import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

private DrawerLayout drawerLayout;
private ListView listView;
private String[] planets;

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
    planets=getResources().getStringArray(R.array.planets);

    listView = (ListView)findViewById(R.id.drawerList);
    listView.setAdapter(new ArrayAdapter<String>                         
    (this,android.R.layout.simple_list_item_1,planets));
    }
 }

答案 1 :(得分:1)

此问题是您尚未初始化ListView

解决方案

创建一个新的ListView对象 -

@Override
protected void onCreate(Bundle savedInstanceState){

    ....

    listView = (ListView)findViewById(R.id.drawerList);
    listView.setAdapter(...);

    }
 }