我正在使用导航抽屉开发新的android应用并扩展 基本活动,并点击此链接 Same Navigation Drawer in different Activities
但是我遇到了异常
进程:edgar.yodgorbek.sportnews,PID:7608 java.lang.RuntimeException:无法启动活动ComponentInfo {edgar.yodgorbek.sportnews / edgar.yodgorbek.sportnews.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.support.v4.widget.DrawerLayout。在空对象引用上的setDrawerListener(android.support.v4.widget.DrawerLayout $ DrawerListener)' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3237) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1929) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:209) 在android.app.ActivityThread.main(ActivityThread.java:7021) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:486) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872) 由以下原因引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout $ DrawerListener)' 在edgar.yodgorbek.sportnews.BaseActivity.onCreate(BaseActivity.java:44) 在edgar.yodgorbek.sportnews.MainActivity.onCreate(MainActivity.java:36) 在android.app.Activity.performCreate(Activity.java:7650) 在android.app.Activity.performCreate(Activity.java:7639) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1295) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3077) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3237) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:81) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1929) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:209) 在android.app.ActivityThread.main(ActivityThread.java:7021) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:486) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:872)
below my BaseActivity class
public class BaseActivity extends Activity {
public DrawerLayout drawerLayout;
public ListView drawerList;
public String[] layers;
Toolbar toolbar;
private ActionBarDrawerToggle drawerToggle;
private Map map;
protected void onCreate(Bundle savedInstanceState) {
// R.id.drawer_layout should be in every activity with exactly the same id.
super.onCreate(savedInstanceState);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle( this, drawerLayout,toolbar,0, 0) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.app_name);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// layers = getResources().getStringArray(R.array.layers_array);
drawerList = (ListView) findViewById(R.id.left_drawer);
View header = getLayoutInflater().inflate(R.layout.base_activity, null);
drawerList.addHeaderView(header, null, false);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.base_activity, android.R.id.text1,
layers));
View footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.base_activity, null, false);
drawerList.addFooterView(footerView);
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
// map.drawerClickEvent(pos);
}
});
}
@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);
}
}
below base_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:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
below my MainActivity
public class MainActivity extends BaseActivity {
public List<Article> articleList = new ArrayList<Article>();
@BindView(R.id.recyclerView)
RecyclerView recyclerView;
private SportNews sportNews;
private ArticleAdapter articleAdapter;
private DrawerLayout drawerLayout;
private ListView drawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
SportInterface sportInterface = SportClient.getApiService();
Call<SportNews> call = sportInterface.getArticles();
call.enqueue(new Callback<SportNews>() {
@Override
public void onResponse(Call<SportNews> call, Response<SportNews> response) {
sportNews = response.body();
if (sportNews != null && sportNews.getArticles() != null) {
articleList.addAll(sportNews.getArticles());
}
articleAdapter = new ArticleAdapter(articleList, sportNews);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(articleAdapter);
}
@Override
public void onFailure(Call<SportNews> call, Throwable t) {
}
});
}
}
below activity_main.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 -->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:0)
实际上,您的BaseActivity没有Layout Set(setContentView),因此DrawerLayout确实为null,因此您不应该在BaseActivity中声明View / findViewById,也不要在所有onCreate()之前在MainActivity触发器中声明super.onCreate()。 )在BaseActivity中,从而导致崩溃
答案 1 :(得分:0)
在super.onCreate(savedInstanceState);
之后写下一行
setContentView(R.layout.activity_main);