【发布时间】:2014-02-19 22:55:15
【问题描述】:
我试图让我的HomeActivity 扩展BaseActivity(如建议的here)这样我就可以在我想要的每个活动中拥有相同的导航抽屉。但是我在 HomeActivity 的 onCreate 方法中得到了一个NullPointerException。这是BaseActivity的代码:
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class BaseActivity extends Activity
{
public DrawerLayout drawerLayout;
public ListView drawerList;
private String[] drawerListEntries;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState)
{
drawerListEntries = new String[] {"Bla bla", "Profile", "Home"};
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_navigation_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);
//layers = getResources().getStringArray(R.array.layers_array);
drawerList = (ListView) findViewById(R.id.drawer_list);
// View header = getLayoutInflater().inflate(R.layout.drawer_list_header, null);
// drawerList.addHeaderView(header, null, false);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawerListEntries));
// View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
// R.layout.drawer_list_footer, null, false);
// drawerList.addFooterView(footerView);
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
//map.drawerClickEvent(pos);
String selectedValue = (String) drawerList.getAdapter().getItem(pos);
Toast.makeText(getBaseContext(), selectedValue, Toast.LENGTH_SHORT).show();
}
});
}
@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);
}
}
这是扩展 BaseActivity 的 HomeActivity 的代码:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.Session;
public class HomeActivity extends BaseActivity {
TextView greeting;
Button orderButton, deliverButton, logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.home, menu);
//return super.onCreateOptionsMenu(menu);
return true;
}
}
我在以下位置收到 NullPointerException:
super.onCreate(savedInstanceState); // HomeActivity
当我点击它时,它会转到:
drawerLayout.setDrawerListener(drawerToggle); // BaseActivity
有谁知道我该如何解决这个问题?
更新:
drawer_list.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" >
<ListView
android:id="@+id/drawer_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="@android:color/transparent"
android:divider="#CCCCCC"
android:dividerHeight="1dp"
android:paddingLeft="2dp" />
</android.support.v4.widget.DrawerLayout>
和drawer_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/drawer_list_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="12dp"
android:textSize="24sp"
android:layout_gravity="left"
android:textColor="@color/belize_hole"
android:fontFamily="sans-serif-light">
</TextView>
</LinearLayout>
【问题讨论】:
-
您似乎在设置内容视图之前尝试查找抽屉视图。
-
@daentech 但这个答案被接受了,发帖人声称它 100% 有效......你建议怎么做?
-
如果将
setContentView(R.layout.activity_home);从HomeActivity移到BaseActivity中,在drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);行之前,是否有效? -
@daentech 我刚刚尝试过,但不起作用。
-
能否请您在问题中发布您的 xml 和 logcat 输出?
标签: android android-activity nullpointerexception navigation-drawer