2015-07-04 24 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