我有问题,我的应用程序甚至没有启动,它只是崩溃。
我非常感谢你的帮助。 :)
我已经尝试使用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>
答案 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(...);
}
}