2015-04-06 116 views
0

我想建立一個簡單的導航抽屜,但我不斷收到這個惱人的錯誤。我對Android相當陌生,所以我真的不知道如何解決/意味着什麼。任何幫助表示讚賞。Android錯誤嘗試調用虛擬方法;我不明白

的Java:

package com.google.helpstl2; 

import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Toast; 

import java.util.List; 

public class Home extends ActionBarActivity implements OnItemClickListener { 

    private DrawerLayout drawerLayout; 
    private ListView listView; 
    private String[] planets; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
     planets = getResources().getStringArray(R.array.planets); 
     listView = (ListView) findViewById(R.id.drawerList); 
     listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, planets)); 
     listView.setOnItemClickListener(this); 

    } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_home, 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(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Toast.makeText(this,planets[position],Toast.LENGTH_LONG).show(); 
    } 
} 

相關的XML?

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/mainContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView android:id="@+id/drawerList" 
     android:layout_width="240dp" 

     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     /> 
</android.support.v4.widget.DrawerLayout> 

logcat的錯誤輸出是:

Process: com.google.helpstl2, PID: 2205 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.helpstl2/com.google.helpstl2.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
+1

發表您的logcat輸出 – Droidman 2015-04-06 01:24:32

+1

感謝您的回答:過程:com.google.helpstl2,PID:2205 了java.lang.RuntimeException:無法開始活動ComponentInfo {com.google.helpstl2/com.google.helpstl2.Home}:java.lang.NullPointerException:嘗試調用null上的虛擬方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'對象引用 – 2015-04-06 01:25:23

+1

看起來你的'ListView'爲空,檢查id'drawerLayout'是否真的存在於你的Activity的佈局中 – Droidman 2015-04-06 01:27:50

回答

0

可能是與內容的XML佈局文件:drawerList

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/mainContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <ListView android:id="@+id/drawerList" 
     android:layout_width="240dp" 

     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     /> 
</android.support.v4.widget.DrawerLayout> 

不是activity_main.xml所以你發現ID時獲得NPE

檢查一次這個東西。

你可以在錯誤行加空檢查:

if(listView != null) {  
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, planets)); 
      listView.setOnItemClickListener(this); 
} 
+0

謝謝!我意識到我的Java文件被設置爲activity_main,而不是activity_home – 2015-04-06 11:46:27

+1

謝謝你,並享受你的工作 – Kushal 2015-04-06 11:46:54

相關問題