2015-07-04 25 views
0

我正在尝试创建导航抽屉,但我不断收到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:尝试上的空对象引用

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 

调用虚拟方法 '无效android.widget.ListView.setAdapter(android.widget.ListAdapter)'编辑:这是我的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> 
+0

你能告诉'activity_main.xml'? –

+0

@ N1继续前进,并添加它 – Rafa

回答

1

编辑:

解决方案是居无定所g DrawerLayout从drawer_layout.xmlactivity_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> 



您需要设置正确的ID在你的XML为您ListView - android:id="@+id/left_drawer",这是因为ID是你在OnCreate指一样,它会导致你的错误。

无论如何,你的实现是错误的。请阅读如何正确执行此操作。

这是很大的解释如何做到这一点 - 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"/> 
+0

我确实在'mDrawerList =(ListView)findViewById(R.id.left_drawer)中设置了正确的ID;'不是吗? – Rafa

+0

不需要。在你的xml中,你设置了'android:id =“@ android:id/list”',而在代码中你希望通过id'left_drawer'找到这个视图,而不是'list'。所以你必须在xml中更改为'android:id =“@ + id/left_drawer”' –

+0

在我的项目的其余部分,我拉和解析了一个json响应,它填充列表视图中的主要活动。我用于抽屉的列表视图位于'drawer_layout.xml'文件中。无论如何,我不会为两个提到的值获得一个空指针错误。 – Rafa