更新时间:2023-01-18 22:26:57
问题:
新意图(背景下,Dashboard.class);
的背景
从不实例从而给您 NPE
解决方案:
新意图(NavigationDrawer.this,Dashboard.class);
的 NavigationDrawer.this
将获得上下文的参照
直接从您的活动类。
I am trying to create a navigation drawer in Android. Here is the code where I set up the navigation drawer:
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);
}
}
}
However, I am getting NullPointerException error message as the LogCat when I click on the first item in navigation drawer:
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
Also, when I tried with Toast.makeText(NavigationDrawer.this, position, Toast.LENGTH_SHORT).show();
, it shows me the same nullPointerException error message. I wonder why is it so.
Thanks in advance.
EDIT
After changed the codes as suggested, I got these error messages:
08-27 11:24:52.127: E/AndroidRuntime(10207): FATAL EXCEPTION: main
08-27 11:24:52.127: E/AndroidRuntime(10207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mymoney/com.example.mymoney.Dashboard}: java.lang.NullPointerException
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.ActivityThread.access$600(ActivityThread.java:127)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.os.Looper.loop(Looper.java:137)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.ActivityThread.main(ActivityThread.java:4512)
08-27 11:24:52.127: E/AndroidRuntime(10207): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 11:24:52.127: E/AndroidRuntime(10207): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 11:24:52.127: E/AndroidRuntime(10207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
08-27 11:24:52.127: E/AndroidRuntime(10207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
08-27 11:24:52.127: E/AndroidRuntime(10207): at dalvik.system.NativeStart.main(Native Method)
08-27 11:24:52.127: E/AndroidRuntime(10207): Caused by: java.lang.NullPointerException
08-27 11:24:52.127: E/AndroidRuntime(10207): at com.example.mymoney.Dashboard.onCreate(Dashboard.java:12)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.Activity.performCreate(Activity.java:4465)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
08-27 11:24:52.127: E/AndroidRuntime(10207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
08-27 11:24:52.127: E/AndroidRuntime(10207): ... 11 more
My dashBoard class as this:
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");
}
}
I am getting the id from this part of my login class:
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;
}
}
}
So basically what I am trying to do is after user logged in, they will be redirected to dashboard. Then, user can also navigate around using the drawer. Am I missing something?
problem:
new Intent(context, Dashboard.class);
The context
is never instantiated thus giving you NPE
solution:
new Intent(NavigationDrawer.this, Dashboard.class);
The NavigationDrawer.this
will get the reference of the context
directly from your activity class.
Android的 - BitmapFactory.de codeByteArray - OutOfMemoryError异常(OOM)
NullPointerException异常使用ShareActionProvider + actionbarsherlock
JavaFx - 线程中的无限异常“JavaFX应用程序线程”显示java.lang.NullPointerException
RuntimeException的:无法启动活动ComponentInfo {}:显示java.lang.NullPointerException。 Android版
java.lang.IllegalArgumentException异常:解析错误 - 无法在Android的解析日期
MapFragment - NullPointerException - Google Maps V2 - Android