我试图通过按导航抽屉中的应用程序图标让用户打开我的导航抽屉。但是当我打开我尝试它的Activity时,我的LogCat在第55行给了我一个NullPointerException。这是我的代码:
AddActivity.java:
package de.hoffmann.pod2go;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class AddActivity extends Activity {
public ActionBarDrawerToggle mDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
// Show the Up button in the action bar.
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(R.string.title_activity_add);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.drawer_title);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true); // Pressing the app icon in the action bar will navigate to the parent activity.
getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add, menu);
return true;
}
}
activty_add.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 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/add_activity_feed_heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_feed_url"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/feed_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="@string/url_hint"
android:layout_alignParentLeft="true" />
<!-- Button bar -->
<LinearLayout android:id="@+id/footer" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_alignParentBottom="true" >
<Button
style="@style/buttonBarButton"
android:text="@string/buttonbar_left"
android:onClick="goBack"/>
<Button
style="@style/buttonBarButton"
android:text="@string/buttonbar_right"/>
</LinearLayout>
</RelativeLayout>
<!-- Navigation Drawer -->
<ListView android:id="@+id/left_drawer"
style="@style/NavigationDrawer"/>
</android.support.v4.widget.DrawerLayout>
此外,我必须将所有代码粘贴到每个活动文件中吗?
答案 0 :(得分:1)
我猜问题在第25行ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
您刚刚使用全局var mDrawerToggle
创建了一个具有相同名称的局部变量
删除第25行中的第一个ActionBarDrawerToggle
可能会使您的代码正常工作
希望得到这个帮助。
答案 1 :(得分:1)
您只需将引用放入本地对象
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(....
只需删除类的名称,以便将引用设置为全局对象值:)
这样说吧
mDrawerToggle = new ActionBarDrawerToggle(...