【问题标题】:Nullpointer with navigation drawer. Not detecting value assignments带有导航抽屉的空指针。未检测到值分配
【发布时间】:2015-07-04 01:16:22
【问题描述】:

我正在尝试创建一个导航抽屉,但我不断收到 NullPointer 错误。当我调试它时,ListViewDrawerLayout 值是空的,即使我为它们分配了各自的值。谁能告诉我怎么了?

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>

【问题讨论】:

  • 你能显示activity_main.xml吗?
  • @N1to 继续添加它

标签: android navigation null-pointer


【解决方案1】:

编辑:

解决方案是将 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"/>

【讨论】:

  • 我确实在mDrawerList = (ListView) findViewById(R.id.left_drawer); 行中设置了正确的ID,不是吗?
  • 没有。在您的 xml 中设置 android:id="@android:id/list",而在代码中您希望通过 id left_drawer 找到此视图,而不是 list。所以你必须把xml改成android:id="@+id/left_drawer"
  • 在我的项目的其余部分中,我正在提取和解析一个 json 响应,该响应填充列表视图中的主要活动。我用于抽屉的列表视图位于drawer_layout.xml 文件中。无论如何,对于提到的两个值,我都不会收到空指针错误。
  • 看。您正在使用 onCreate 中的函数 setContentView(R.layout.activity_main); 设置 MainActivity 的布局。然后,您尝试获取对小部件 mDrawerList = (ListView) findViewById(R.id.left_drawer); 的引用。它开始在 activity_main.xml 中搜索 ID 为 left_drawer 的项目,但由于此布局中不存在它而无法找到。
  • 所以最初我的drawer_layout 不在我的activity_main 中,我发现它必须是。我把它放在那里,所以现在我有两个ListViews,一个填充主布局和一个填充抽屉布局。但仍然会出现同样的错误。
猜你喜欢
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-09
  • 1970-01-01
  • 2015-03-04
  • 1970-01-01
  • 2013-06-14
  • 2018-10-15
相关资源
最近更新 更多