java.lang.NullPointerException 和未知来源
java.lang.NullPointerException and Unknown Source
我很难解决我的代码中的一些问题。我在 phone 上对其进行了测试,效果很好。但是,如果我在 Galaxy Tab 4 上对其进行测试,它会在 onCreate() 方法中为我提供 java.lang.NullPointerException 和未知来源。
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainxml);
Button butns = (Button) findViewById(R.id.sb1);
butns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub1.class);
startActivity(intent);
}
});
Button butnss = (Button) findViewById(R.id.sb2);
butnss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub2.class);
startActivity(intent);
}
});
Button butsns = (Button) findViewById(R.id.sb3);
butsns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub3.class);
startActivity(intent);
}
});
Button quiz = (Button) findViewById(R.id.quiz);
quiz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(intent);
}
});
ImageButton butsn1s = (ImageButton) findViewById(R.id.info);
butsn1s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Info.class);
startActivity(intent);
}
});
ImageButton btn = (ImageButton) findViewById(R.id.fb);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/SharpMind-software/540118499454551?ref=hl"));
startActivity(myWebLink);
}
});
ImageButton cnt = (ImageButton) findViewById(R.id.count);
cnt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent count = new Intent(getApplicationContext(),Count.class);
startActivity(count);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.settings:
Intent settings = new Intent(getApplicationContext(), Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
错误如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sharpmind.Romana_pentru_BAC_PRO/com.sharpmind.Romana_pentru_BAC_PRO.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access0(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.sharpmind.Romana_pentru_BAC_PRO.MainActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
... 11 more
谢谢。
祝你有美好的一天!
尝试更改package name
,碰巧有些包名不被android接受。我不知道为什么,但它甚至不接受 com.android.wifi
几天前遇到同样的问题并通过使用不同的包名称成功解决
并确保使用 'lower cases' 并在开头添加 com
。
感谢您的帮助,如果问题再次出现,我将知道如何尝试。
我找到了。我的 main.xml 布局大型资源不包含上面声明的按钮之一。
我现在可能看起来像个傻瓜,但它确实发生了。
再次感谢您的回答和评论。
我很难解决我的代码中的一些问题。我在 phone 上对其进行了测试,效果很好。但是,如果我在 Galaxy Tab 4 上对其进行测试,它会在 onCreate() 方法中为我提供 java.lang.NullPointerException 和未知来源。
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainxml);
Button butns = (Button) findViewById(R.id.sb1);
butns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub1.class);
startActivity(intent);
}
});
Button butnss = (Button) findViewById(R.id.sb2);
butnss.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub2.class);
startActivity(intent);
}
});
Button butsns = (Button) findViewById(R.id.sb3);
butsns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Sub3.class);
startActivity(intent);
}
});
Button quiz = (Button) findViewById(R.id.quiz);
quiz.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), QuizActivity.class);
startActivity(intent);
}
});
ImageButton butsn1s = (ImageButton) findViewById(R.id.info);
butsn1s.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), Info.class);
startActivity(intent);
}
});
ImageButton btn = (ImageButton) findViewById(R.id.fb);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myWebLink = new Intent(Intent.ACTION_VIEW);
myWebLink.setData(Uri.parse("https://www.facebook.com/pages/SharpMind-software/540118499454551?ref=hl"));
startActivity(myWebLink);
}
});
ImageButton cnt = (ImageButton) findViewById(R.id.count);
cnt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent count = new Intent(getApplicationContext(),Count.class);
startActivity(count);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.settings:
Intent settings = new Intent(getApplicationContext(), Settings.class);
startActivity(settings);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
错误如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sharpmind.Romana_pentru_BAC_PRO/com.sharpmind.Romana_pentru_BAC_PRO.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.access0(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5479)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.sharpmind.Romana_pentru_BAC_PRO.MainActivity.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
... 11 more
谢谢。 祝你有美好的一天!
尝试更改package name
,碰巧有些包名不被android接受。我不知道为什么,但它甚至不接受 com.android.wifi
几天前遇到同样的问题并通过使用不同的包名称成功解决
并确保使用 'lower cases' 并在开头添加 com
。
感谢您的帮助,如果问题再次出现,我将知道如何尝试。 我找到了。我的 main.xml 布局大型资源不包含上面声明的按钮之一。 我现在可能看起来像个傻瓜,但它确实发生了。 再次感谢您的回答和评论。