[英]Crashing when creating navigation panel
I am attempting to make a navigation panel in Android. 我正在尝试在Android中制作一个导航面板。 I followed the instructions from http://developer.android.com/training/implementing-navigation/nav-drawer.html .
我遵循了http://developer.android.com/training/implementing-navigation/nav-drawer.html中的说明。 However the application is crashing by a Null Pointer Exception.
但是,应用程序由于空指针异常而崩溃。
MainActivity.java MainActivity.java
//import statements
public class MainActivity extends ActionBarActivity {
private String[] mPlanetTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlanetTitles = getResources().getStringArray(R.array.nav_drawer_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,
R.layout.drawer_listview_item, mPlanetTitles));
// Set the list's click listener
// mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
>
<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" />
<!-- 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"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
drawer_listview_item.xml drawer_listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:paddingRight="40dp"/>
</LinearLayout>
I am receiving a NULL pointer Pointer Exception. 我收到了一个空指针指针异常。 Can anyone help me pinpoint the error?
谁能帮我查明错误?
change 更改
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,
R.layout.drawer_listview_item, mPlanetTitles));
to 至
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_listview_item,
R.id.title, mPlanetTitles));
because your custom layout for list is drawer_listview_item
and is not activity_main
因为您的列表的自定义布局是
drawer_listview_item
列表drawer_listview_item
,而不是activity_main
for more info see section 6 @ Vogella Tutorial 有关更多信息,请参见第6节@ Vogella教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.