我在null pointer exception
drawerLayout=(DrawerLayout) findViewById(R.layout.drawer_layout);
请帮帮我。为什么它返回空指针异常?我不知道自己做错了什么。
package com.example.prototype;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
DrawerLayout drawerLayout;
ActionBar action_bar;
ListView navigation_list;
ActionBarDrawerToggle drawerToggle;
String items[];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
initComponents();
action_bar=getActionBar();
setNavigationList();
}
@SuppressWarnings("deprecation")
private void setNavigationList() {
int open=R.string.navigation_open;
int close=R.string.navigation_close;
navigation_list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
drawerToggle=new ActionBarDrawerToggle(this,drawerLayout, R.drawable.ic_menu_white_18dp,open,close){
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
action_bar.show();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
action_bar.hide();
}
};
if(drawerLayout==null){
Toast.makeText(getApplicationContext(), "null", Toast.LENGTH_LONG).show();
} else {
drawerLayout.setDrawerListener(drawerToggle);
action_bar.setDisplayHomeAsUpEnabled(true);
action_bar.setHomeButtonEnabled(true);
}
}
private void initComponents() {
// TODO Auto-generated method stub
drawerLayout=(DrawerLayout) findViewById(R.layout.drawer_layout);
action_bar=getActionBar();
navigation_list=(ListView) findViewById(R.id.left_drawer);
items=getResources().getStringArray(R.array.navigation_drawer);
if(drawerLayout==null){
Toast.makeText(getApplicationContext(), "null0", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.search_menu) {
Toast.makeText(getApplicationContext(), "Searched Clicked ", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.share_menu) {
Toast.makeText(getApplicationContext(), "Share Clicked ", Toast.LENGTH_LONG).show();
return true;
}
if (id == R.id.navigation_menu) {
Toast.makeText(getApplicationContext(), "Navigation Clicked ", Toast.LENGTH_LONG).show();
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的Xml file
<?xml version="1.0" encoding="utf-8"?>
<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">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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="5dp"
android:background="#E1E1E1"/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:3)
必须是
drawerLayout=(DrawerLayout) findViewById(R.id.drawer_layout);
R.id R.layout 的实例,因为您指的是元素,而不是XML文件。