问题
I'm having a hard time fixing some issues with my code. I tested it on my phone and it works good. But if I test it on a Galaxy Tab 4, it gives me java.lang.NullPointerException and unknown source in the onCreate() method.
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);
}
}
}
And below is the error:
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.access$900(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
Thanks. Have a nice day!
回答1:
Try to change the package name
, it happens that some package names are not accepted by android. I don't know why but it even doesn't accept com.android.wifi
Faced same problem few days back and successfully solved by using a different package name
and make sure you use 'lower cases' and add com
in the starting.
回答2:
Thanks for help, now if the problem appears again I will know what to try. I've found it. My main.xml layout large resource was not containing one of the buttons declared above. I might look like a fool now, but it happens. Thanks again for answers and comments.
来源:https://stackoverflow.com/questions/31046394/java-lang-nullpointerexception-and-unknown-source