Java Android NullPointerException错误消息
我正在尝试在Android中创建一个导航抽屉。下面是我设置导航抽屉的代码:Java Android NullPointerException错误消息,java,android,nullpointerexception,navigation-drawer,Java,Android,Nullpointerexception,Navigation Drawer,我正在尝试在Android中创建一个导航抽屉。下面是我设置导航抽屉的代码: public class NavigationDrawer extends Activity { Context context; private String[] drawerListViewItems; private DrawerLayout drawerLayout; private ListView drawerListView; private ActionBarDrawerToggle actionBar
public class NavigationDrawer extends Activity {
Context context;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
protected DrawerLayout fullLayout;
protected FrameLayout actContent;
@Override
public void setContentView(final int layoutResID) {
fullLayout = (DrawerLayout) getLayoutInflater().inflate(
R.layout.navigation_drawer, null); // Your base layout here
actContent = (FrameLayout) fullLayout.findViewById(R.id.act_content);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in navigation_drawer.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option add shadow the right edge of the drawer
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
drawerListView.bringToFront();
//drawerListView.requestLayout();
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
// true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
switch (position) {
//Dashboard
case 0:
Intent dashboardIntent = new Intent(context, Dashboard.class);
startActivity(dashboardIntent);
break;
//Account
case 1:
break;
//Setting
case 2:
break;
//Logout
case 3:
Toast.makeText(NavigationDrawer.this, "You have logged out!", Toast.LENGTH_SHORT)
.show();
System.exit(0);
break;
}
drawerLayout.closeDrawer(drawerListView);
}
}
}
8-27 11:10:55.932: E/AndroidRuntime(7088): FATAL EXCEPTION: main
08-27 11:10:55.932: E/AndroidRuntime(7088): java.lang.NullPointerException
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.content.ComponentName.<init>(ComponentName.java:75)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.content.Intent.<init>(Intent.java:3201)
08-27 11:10:55.932: E/AndroidRuntime(7088): at com.example.mymoney.NavigationDrawer$DrawerItemClickListener.onItemClick(NavigationDrawer.java:105)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.widget.AbsListView.performItemClick(AbsListView.java:1185)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2713)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.widget.AbsListView$1.run(AbsListView.java:3468)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.os.Handler.handleCallback(Handler.java:605)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.os.Looper.loop(Looper.java:137)
08-27 11:10:55.932: E/AndroidRuntime(7088): at android.app.ActivityThread.main(ActivityThread.java:4512)
08-27 11:10:55.932: E/AndroidRuntime(7088): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 11:10:55.932: E/AndroidRuntime(7088): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 11:10:55.932: E/AndroidRuntime(7088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
08-27 11:10:55.932: E/AndroidRuntime(7088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
08-27 11:10:55.932: E/AndroidRuntime(7088): at dalvik.system.NativeStart.main(Native Method)
08-27 11:11:23.643: I/Process(7088): Sending signal. PID: 7088 SIG: 9
我的仪表板类如下所示:
public class Dashboard extends NavigationDrawer{
int id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
id = getIntent().getExtras().getInt("userID");
}
}
我正在从登录类的这一部分获取id:
public void onLogInClicked(View view) {
// TODO Auto-generated method stub
DatabaseAdapter mDbHelper = new DatabaseAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
UserController uc = new UserController(mDbHelper.open());
ArrayList<UserModel> ulogin = uc.getLogin();
boolean login = false;
for (int i = 0; i < ulogin.size(); i++) {
if (txtUserName.getText().toString()
.equals(ulogin.get(i).getUserName().toString())
&& txtPwd.getText().toString()
.equals(ulogin.get(i).getPassword().toString())) {
Toast.makeText(MainActivity.this, "Login Successfully!",
Toast.LENGTH_SHORT).show();
loginintent = new Intent(context, Dashboard.class);
loginintent.putExtra("userName", ulogin.get(i)
.getUserName().toString());
startActivity(loginintent);
login = true;
break;
}
}
}
public void onLogInClicked(视图){
//TODO自动生成的方法存根
DatabaseAdapter mDbHelper=新的DatabaseAdapter(此);
mDbHelper.createDatabase();
mDbHelper.open();
UserController uc=新的UserController(mDbHelper.open());
ArrayList ulogin=uc.getLogin();
布尔登录=false;
对于(int i=0;i
所以基本上我想做的是在用户登录后,他们会被重定向到仪表板。然后,用户还可以使用抽屉四处导航。我遗漏了什么吗?问题:
new Intent(context, Dashboard.class);
new Intent(NavigationDrawer.this, Dashboard.class);
上下文
从未被实例化,因此为您提供NPE
解决方案:
new Intent(context, Dashboard.class);
new Intent(NavigationDrawer.this, Dashboard.class);
NavigationDrawer。此
将直接从活动类获取上下文的引用。第105行中有什么?这两行:Intent Dashboard Intent=新Intent(context,Dashboard.class);startActivity(仪表板意图);通常只有一行可以是第105行!是的,是105和106。问题发生在我添加了这两行之后,但在我更改了这两行之后,它仍然会给我NullPointerException。我会发布日志,给我几秒钟。@9Dollars99Cents发布你的仪表板
,并告诉我行12
@9Dollars99Cents在你将代码更改为我的代码后,NPE现在在你的仪表板
类别中。我已经添加了代码。你能帮我看一下吗?@9美元99美分把putExtra
放在你的onItemClick