带导航抽屉的空指针。不检测值分配
Nullpointer with navigation drawer. Not detecting value assignments
我正在尝试创建导航抽屉,但我一直收到 NullPointer 错误。当我调试它时,ListView
和 DrawerLayout
值为空,即使我为它们分配了各自的值。谁能告诉我怎么了?
public class MainActivity extends ListActivity {
private ArrayList<String> mTitles = new ArrayList<String>();
private ArrayList<String> mUrls = new ArrayList<String>();
private ArrayList<String> mAbstract = new ArrayList<String>();
public ArrayList<String> mDrawerTitles = new ArrayList<String>();
public DrawerLayout mDrawerLayout;
public ListView mDrawerList;
ProgressDialog progressDialog;
public final String TOPSTORYKEY = "878029f7fee45ecd61ca3f52ea027186:19:72302140";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
mDrawerTitles.add("Politics");
mDrawerTitles.add("Technology");
mDrawerTitles.add("Sports");
mDrawerTitles.add("Books");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
//DrawerListAdapter drawerAdapter = new DrawerListAdapter(this, mDrawerTitles);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//new HttpJSONLoader().execute();
}
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
编辑:这是我的 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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/empty"
android:text="No results"
android:layout_centerInParent="true"/>
<!-- 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>
编辑:
解决方案是将 DrawerLayout 从 drawer_layout.xml
移动到 activity_main.xml
并使布局只有两个子项,其中第一个是 MainActivity UI,第二个是导航抽屉。
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No results" />
</RelativeLayout>
<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>
您需要在 XML 中为您的 ListView
- android:id="@+id/left_drawer"
设置正确的 ID,这与您在 OnCreate
中引用的 ID 相同,这会导致你的错误。
不管怎样,你的实现是错误的。请阅读如何正确执行此操作。
这里很好地解释了如何做到这一点 - link
<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" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_drawer"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No results"
android:layout_centerInParent="true"/>
我正在尝试创建导航抽屉,但我一直收到 NullPointer 错误。当我调试它时,ListView
和 DrawerLayout
值为空,即使我为它们分配了各自的值。谁能告诉我怎么了?
public class MainActivity extends ListActivity {
private ArrayList<String> mTitles = new ArrayList<String>();
private ArrayList<String> mUrls = new ArrayList<String>();
private ArrayList<String> mAbstract = new ArrayList<String>();
public ArrayList<String> mDrawerTitles = new ArrayList<String>();
public DrawerLayout mDrawerLayout;
public ListView mDrawerList;
ProgressDialog progressDialog;
public final String TOPSTORYKEY = "878029f7fee45ecd61ca3f52ea027186:19:72302140";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
mDrawerTitles.add("Politics");
mDrawerTitles.add("Technology");
mDrawerTitles.add("Sports");
mDrawerTitles.add("Books");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
//DrawerListAdapter drawerAdapter = new DrawerListAdapter(this, mDrawerTitles);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDrawerTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
//new HttpJSONLoader().execute();
}
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
编辑:这是我的 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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/empty"
android:text="No results"
android:layout_centerInParent="true"/>
<!-- 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>
编辑:
解决方案是将 DrawerLayout 从 drawer_layout.xml
移动到 activity_main.xml
并使布局只有两个子项,其中第一个是 MainActivity UI,第二个是导航抽屉。
<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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="No results" />
</RelativeLayout>
<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>
您需要在 XML 中为您的 ListView
- android:id="@+id/left_drawer"
设置正确的 ID,这与您在 OnCreate
中引用的 ID 相同,这会导致你的错误。
不管怎样,你的实现是错误的。请阅读如何正确执行此操作。
这里很好地解释了如何做到这一点 - link
<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" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/left_drawer"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No results"
android:layout_centerInParent="true"/>