我正在尝试使用导航抽屉开发一个简单的应用程序。 我尽我所能,看到这么多文章,但我失败了 我的代码没有错误,但是当我在模拟器中运行我的应用程序时,它显示运行时错误... plzz帮帮我
这是我的logcat文件
12-19 15:07:20.562: E/AndroidRuntime(1067): FATAL EXCEPTION: main
12-19 15:07:20.562: E/AndroidRuntime(1067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dhruv.sms/com.dhruv.sms.MainActivity}: java.lang.NullPointerException
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.os.Looper.loop(Looper.java:137)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-19 15:07:20.562: E/AndroidRuntime(1067): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 15:07:20.562: E/AndroidRuntime(1067): at java.lang.reflect.Method.invoke(Method.java:511)
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:560)
12-19 15:07:20.562: E/AndroidRuntime(1067): at dalvik.system.NativeStart.main(Native Method)
12-19 15:07:20.562: E/AndroidRuntime(1067): Caused by: java.lang.NullPointerException
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.dhruv.sms.MainActivity.onCreate(MainActivity.java:80)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.Activity.performCreate(Activity.java:5104)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-19 15:07:20.562: E/AndroidRuntime(1067): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-19 15:07:20.562: E/AndroidRuntime(1067): ... 11 more
这是我的java文件
package com.dhruv.sms;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
// Within which the entire activity is enclosed
private DrawerLayout mDrawerLayout;
// ListView represents Navigation Drawer
private ListView mDrawerList;
// ActionBarDrawerToggle indicates the presence of Navigation Drawer in the action bar
private ActionBarDrawerToggle mDrawerToggle;
// Title of the action bar
private String mTitle = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = "App title";
getActionBar().setTitle(mTitle);
// Getting reference to the DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.conversation_list);
// Getting reference to the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_launcher, R.string.drawer_open,
R.string.drawer_close) {
/** Called when drawer is closed */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("App title");
invalidateOptionsMenu();
}
};
// Setting DrawerToggle on DrawerLayout
mDrawerLayout.setDrawerListener(mDrawerToggle);
// Creating an ArrayAdapter to add items to the listview mDrawerList
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),
R.layout.drawer_list_item, getResources().getStringArray(R.array.sidebar_list_array));
// Setting the adapter on mDrawerList
mDrawerList.setAdapter(adapter);
// Enabling Home button
getActionBar().setHomeButtonEnabled(true);
// Enabling Up navigation
getActionBar().setDisplayHomeAsUpEnabled(true);
// Setting item click listener for the listview mDrawerList
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Getting an array of rivers
String[] menuItems = getResources().getStringArray(R.array.sidebar_list_array);
// Currently selected river
mTitle = menuItems[position];
// Creating a fragment object
Fragment rFragment = new Fragment();
// Passing selected item information to fragment
// Getting reference to the FragmentManager
FragmentManager fragmentManager = getFragmentManager();
// Creating a fragment transaction
FragmentTransaction ft = fragmentManager.beginTransaction();
// Adding a fragment to the fragment transaction
ft.replace(R.id.content_frame, rFragment);
// Committing the transaction
ft.commit();
// Closing the drawer
mDrawerLayout.closeDrawer(mDrawerList);
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:1)
变量mDrawerList
可能未正确初始化。它可以是null
,从而导致NullPointerException
。
答案 1 :(得分:0)
您的代码中存在错误。堆栈跟踪显示了这一点。这是相关信息
12-19 15:07:20.562: E/AndroidRuntime(1067): Caused by: java.lang.NullPointerException
12-19 15:07:20.562: E/AndroidRuntime(1067): at com.dhruv.sms.MainActivity.onCreate(MainActivity.java:80)
第80行显示正在访问的mDrawerList。它是空的
答案 2 :(得分:0)
错误出现在代码的这一部分
mDrawerList.setAdapter(adapter);
mDrawerList引用可能错误或适配器错误
答案 3 :(得分:0)
private ListView mDrawerList=new ListView();
您尚未初始化ListView。