[英]App crashes while changing the orientation from landscape to portrait
錯誤 **2020-03-01 17:36:58.959 6589-6589/com.studenthelper.bscit E/AndroidRuntime:致命異常:主進程:com.studenthelper.bscit,PID:6589 java.lang.RuntimeException:無法啟動活動 ComponentInfo{com.studenthelper.bscit/com.studenthelper.bscit.MainActivity}:java.lang.NullPointerException:嘗試在空對象上調用虛擬方法“void android.widget.TextView.setText(java.lang.CharSequence)”參考 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) 在 android.app.ActivityThread.main(ActivityThread.java:6692) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit .java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 引起:java.lang.NullPointerException:嘗試調用虛擬方法 'void android.widget.TextView.setText(java. lang.CharSequence)' 在 com.studenthelper.bscit.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:7140) 在 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) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在 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));
}
根據您的崩潰日志:
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)
這意味着你的textView
TextView textView=findViewById(R.id.logotext);
未從布局R.layout.activity_main 中找到,因此當您嘗試textView.setText(Text);
它實際上是null並且發生異常。
所以,我會建議
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
在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);
}
總結一下問題:您定義了兩個布局,其中一個沒有TextView
。 從您的代碼中,顯然崩潰是由在空對象上調用.setText()
引起的。
最好的方法是始終將缺少的TextView
添加回另一個布局,因此所有布局都包含相同的 ID 集。 這可以避免由findViewById()
引起的錯誤。 在不需要TextView
的布局中,您可以簡單地設置它的android:visibility="gone"
來隱藏它。
或者回到您的代碼,如果您知道TextView
並不總是存在,您可以在調用.setText()
之前簡單地檢查您的textView
變量是否為null
。 這也可以避免NullPointerException
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.