我一直在寻找SO以及Google搜索,并决定扩展BaseActivity的活动是在我的项目中集成导航抽屉的最佳方式。每当我启动我的应用程序时,它会立即崩溃。下面我已经包含了我的BaseActivity代码,问题在于这一行:
drawerLayout.setDrawerListener(drawerToggle);
导致NullPointerException
public class BaseActivity extends Activity
{
public DrawerLayout drawerLayout;
public ListView drawerList;
public String[] layers;
private ActionBarDrawerToggle drawerToggle;
Intent twitch = new Intent(this, TwitchActivity.class);
Intent community = new Intent(this, CommunityActivity.class);
Intent esports = new Intent(this, ESportsActivity.class);
Intent home = new Intent(this, MainActivity.class);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.drawer_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBar actionBar = getActionBar();
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_launcher, 0, 0)
{
public void onDrawerClosed(View view)
{
actionBar.setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
actionBar.setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
layers = getResources().getStringArray(R.array.layers);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
layers));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
String selected = arg0.getItemAtPosition(pos).toString();
if(selected.equals("Twitch"))
startActivity(twitch);
if(selected.equals("Community"))
startActivity(community);
if(selected.equals("ESports"))
startActivity(esports);
if(selected.equals("Home"))
startActivity(home);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
我的主要活动
public class MainActivity extends BaseActivity {
private Spinner spinner;
public static String region;
public static String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//.........
我的drawer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Nav Drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111111"/>
</android.support.v4.widget.DrawerLayout>
完全错误:
04-10 20:17:25.409 4548-4548/edu.appdesign.leaguestats E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: edu.appdesign.leaguestats, PID: 4548
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.appdesign.leaguestats/edu.appdesign.leaguestats.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d type #0x12 is not valid
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$800(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f08003d type #0x12 is not valid
at android.content.res.Resources.loadXmlResourceParser(Resources.java)
at android.content.res.Resources.getLayout(Resources.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java)
at android.app.Activity.setContentView(Activity.java)
at edu.appdesign.leaguestats.BaseActivity.onCreate(BaseActivity.java:34)
at edu.appdesign.leaguestats.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$800(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您的onCreate()
必须在调用findViewById()
之前调用setContentView()设置布局,才能从该布局中获取视图。并确保传递给setContentView()
正确的布局资源(它应该是'R.layout.xxxxx',其中'xxxxx'是XML布局文件的名称,没有'.xml'扩展名)。