我想创建一个BaseActivity
Navigation Drawer
来扩展其他活动。 (对所有活动使用一个抽屉)
我搜索并尝试了许多代码,例如this link,但我仍然有:
无法启动活动(NullPointExeption)错误!
我也在MainActivity
:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
super.onCreateDrawer();
但它不起作用:(
BaseActivity:
public abstract class BaseActivity extends Activity
{
public DrawerLayout drawerLayout;
public String[] layers;
private ActionBarDrawerToggle drawerToggle;
protected void onCreateDrawer()
{
// R.id.drawer_layout should be in every activity with exactly the same id.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(R.string.hello_world);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@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);
}
}
MainActivity:
public class MainActivity extends BaseActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreateDrawer();
}
}
drawer_activity.xml
<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">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Put what you want as your normal screen in here, you can also choose for a linear layout or any other layout, whatever you prefer -->
</FrameLayout>
<!-- The navigation 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="#111"/>
logcat的:
09-07 07:12:47.308: E/AndroidRuntime(2620): FATAL EXCEPTION: main
09-07 07:12:47.308: E/AndroidRuntime(2620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.os.Looper.loop(Looper.java:137)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-07 07:12:47.308: E/AndroidRuntime(2620): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 07:12:47.308: E/AndroidRuntime(2620): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 07:12:47.308: E/AndroidRuntime(2620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-07 07:12:47.308: E/AndroidRuntime(2620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 07:12:47.308: E/AndroidRuntime(2620): at dalvik.system.NativeStart.main(Native Method)
09-07 07:12:47.308: E/AndroidRuntime(2620): Caused by: java.lang.NullPointerException
09-07 07:12:47.308: E/AndroidRuntime(2620): at com.example.test.NavigationActivity.onCreateDrawer(NavigationActivity.java:38)
09-07 07:12:47.308: E/AndroidRuntime(2620): at com.example.test.MainActivity.onCreate(MainActivity.java:11)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.Activity.performCreate(Activity.java:5008)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-07 07:12:47.308: E/AndroidRuntime(2620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
解决方案是什么?! 感谢。
答案 0 :(得分:2)
NPE
可能很容易导致getActionBar()
,具体取决于Android版本和您的清单配置。我强烈建议您延长AppCompatActivity
而不是Activity
,并使用getSupportActionBar()
代替getActionBar()
。请注意,两者都可以返回null,具体取决于您在清单上选择的主题。
答案 1 :(得分:0)
onCreateDrawer()方法中出现错误。可能是因为:
不使用活动,而是使用 AppCompatActivity
而不是这一行
drawerToggle = new ActionBarDrawerToggle((Activity)this, drawerLayout,R.drawable.ic_drawer,0,0)
像这样做
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout, R.drawable.ic_drawer,0,0)