简体   繁体   中英

App crashes while changing the orientation from landscape to portrait

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.ex ecute(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.ActivityT hread.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));
}

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

  1. Verify textVIew Id is same in the layout? and
  2. Set the getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); before the setContentView(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);

}

To summarise the problem: You have defined two layouts, which one of them does not have a TextView . From your code obviously the crash is caused by calling .setText() on a null object.

The best way is always to add back the missing TextView back to your another layout, so all your layouts contains the same set of IDs. This can avoid bugs caused by findViewById() . In the layout which you do not need the TextView , you can simply set its android:visibility="gone" to hide it.

Alternatively back to your code, if you know the TextView is not always there, you can simply check if your textView variable is null or not, before calling .setText() . This can also avoid the NullPointerException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM