我正在嘗試創建導航抽屜,但我不斷收到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:嘗試上的空對象引用
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>
你能告訴'activity_main.xml'? –
@ N1繼續前進,並添加它 – Rafa