好的,所以我有两个活动,并且我想打开活动“单击按钮时插入”。
但是它发出了一条消息,不幸的是,活动已意外停止或发生了任何事情。
来自主活动的部分代码,其中按钮正在调用意图:
//The intent typed earlier in the activity
Intent myIntent = new Intent(MainActivity.this, Insert.class);
private OnClickListener onClick() {
return new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addcolumnbutton:
if(value==k||value==10)
{
Toast.makeText(getApplicationContext(), "Maximum number of Columns created",
Toast.LENGTH_SHORT).show();
}
else {
k++;
mLayout.addView(createNewLayout());
}
break;
case R.id.createtablebutton:
if(value==k) {
MainActivity.this.startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "Incorrect number of Columns created",
Toast.LENGTH_SHORT).show();
}
break;
}
}
};
}
我的LogCat在这里:
03-02 17:21:34.342 12789-12789/com.cambiopune.tosdatabase D/AndroidRuntime﹕ Shutting down VM
03-02 17:21:34.345 12789-12789/com.cambiopune.tosdatabase E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cambiopune.tosdatabase, PID: 12789
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cambiopune.tosdatabase/com.cambiopune.tosdatabase.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:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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:131)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:3996)
at com.cambiopune.tosdatabase.MainActivity.<init>(MainActivity.java:34)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
最佳答案
只需在代码中删除此行:
private OnClickListener onClick() {
return new OnClickListener() {
它应该只是:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addcolumnbutton:
if(value==k||value==10)
{
Toast.makeText(getApplicationContext(), "Maximum number of Columns created",
Toast.LENGTH_SHORT).show();
}
else {
k++;
mLayout.addView(createNewLayout());
}
break;
case R.id.createtablebutton:
if(value==k) {
MainActivity.this.startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "Incorrect number of Columns created",
Toast.LENGTH_SHORT).show();
}
break;
}
}
关于java - Android Null对象引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28809569/