问题
ERROR **2020-03-01 17:36:58.959 6589-6589/com.studenthelper.bscit E/AndroidRuntime: FATAL EXCEPTION: main Process: com.studenthelper.bscit, PID: 6589 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.studenthelper.bscit/com.studenthelper.bscit.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694) at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:7140) at android.app.Activity.performCreate(Activity.java:7131) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049) at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:4785) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4694) at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) **
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
public String getEmoji(int uni)
{
return new String(Character.toChars(uni));
}
回答1:
As per your crash logs:
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at
com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:20)
It means your textView
TextView textView=findViewById(R.id.logotext);
Is not found from the layout R.layout.activity_main due to which when you try to textView.setText(Text); it's actually null and exception occurs.
So, I'll suggest to
- Verify textVIew Id is same in the layout? and
- Set the
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);before thesetContentView(R.layout.activity_main);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
TextView textView=findViewById(R.id.logotext);
int unicode=0x1F4A1;
String emoji=getEmoji(unicode);
String Text="Bsc"+emoji+"T";
textView.setText(Text);
}
来源:https://stackoverflow.com/questions/60458106/app-crashes-while-changing-the-orientation-from-landscape-to-portrait