我是Android的初学者并遇到问题 我正在尝试从登录活动的登录按钮打开主要活动时显示以上错误。
当按下登录按钮时,应用程序停止并显示以下错误
在android监视器中显示以下错误
10-20 18:47:23.314 13432-13432/com.antherx.prasenjit.ihdfbachatgat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.antherx.prasenjit.ihdfbachatgat, PID: 13432
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.antherx.prasenjit.ihdfbachatgat/com.antherx.prasenjit.ihdfbachatgat.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4358)
at com.antherx.prasenjit.ihdfbachatgat.MainActivity.<init>(MainActivity.java:26)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2629)
at android.app.ActivityThread.access$800(ActivityThread.java:182)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5706)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)
这是登录活动代码是他们在此代码中的任何问题
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Please do not
Email = (EditText)findViewById(R.id.email);
Password = (EditText)findViewById(R.id.password);
String wel_on_login = "Welcome On IHDF Bachat Gat app";
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_login,Toast.LENGTH_LONG);
welcome.show();
final Button signIn = (Button)findViewById(R.id.email_sign_in_button);
final Button register = (Button)findViewById(R.id.registration) ;
final Intent main_trans = new Intent(this, MainActivity.class);
final Intent reg_trans = new Intent(this, register.class);
signIn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
startActivity(main_trans);
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(reg_trans);
}
});
这是主要的活动代码是他们在这里的任何问题
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.home_toolbar);
setSupportActionBar(toolbar);
newMember = (MenuItem)findViewById(R.id.new_member);
newProfile = (MenuItem)findViewById(R.id.profile) ;
Drawer =(DrawerLayout)findViewById(R.id.drawerLayout);
sToggle = new ActionBarDrawerToggle(this,Drawer,R.string.open,R.string.close);
Grp_ico = (ImageView)findViewById(R.id.grp_ico);
Drawer.addDrawerListener(sToggle);
sToggle.syncState();
getSupportActionBar();
String wel_on_main = "You are successfully on Main Home";
Toast welcome = Toast.makeText(getApplicationContext(),wel_on_main,Toast.LENGTH_LONG);
welcome.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.new_member :
startActivity(new_new_member);
case R.id.profile :
startActivity(new_new_profile);
case R.id.setting :
startActivity(new_new_setting);
case R.id.meeting :
startActivity(new_new_meeting);
default:
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
对我来说,我试图在没有基本上下文的情况下使用该应用程序。
所以我在我的应用程序覆盖:
@Override
protected void attachBaseContext(Context newBase) {
}
//instead of calling its super:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);}
答案 1 :(得分:-1)
从
更改[account]
user=user1
pass=pass1
[account]
user=user2
pass=pass2
[account]
user=user3
pass=pass3
expdate=2016
getApplicationContext()
到Toast welcome = Toast.makeText(getApplicationContext(),wel_on_main,Toast.LENGTH_LONG);
或最好是getBaseContext()