在android中使用多屏时应用程序崩溃
Posted
技术标签:
【中文标题】在android中使用多屏时应用程序崩溃【英文标题】:App crashes while using multiscreen in android 【发布时间】:2022-01-21 13:49:53 【问题描述】:我正在尝试在 android studio 中构建一个多屏应用程序,但不幸的是,它在运行期间崩溃了。该应用程序在主屏幕上运行良好,它不会在 MainActivity
上崩溃,但在 MainActivity2
上它会崩溃。
这是我的 MainActivity Java 代码:
package com.example.scrollapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
EditText editText;
public static final String EXTRA_NAME ="com.example.scrollapp.extra.NAME";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.editTextTextPersonName3);
String nametext = editText.getText().toString();
public void openActivity(View v)
Toast.makeText(MainActivity.this, "opening second activity", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this,MainActivity2.class);
editText=findViewById(R.id.editTextTextPersonName3);
String nametext = editText.getText().toString();
intent.putExtra(EXTRA_NAME,nametext);
startActivity(intent);
这是 MainActivity2 的 Java 代码:
package com.example.scrollapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity
TextView textView;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView.findViewById(R.id.textView);
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_NAME);
textView.setText("your name is "+ name);
logcat中显示的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.scrollapp, PID: 7978
java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.scrollapp/com.example.scrollapp.MainActivity2: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.TextView.findViewById(int)' on a null object reference
at com.example.scrollapp.MainActivity2.onCreate(MainActivity2.java:18)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/Process: Sending signal. PID: 7978 SIG: 9
【问题讨论】:
【参考方案1】:TextView
的初始化不正确。正确的初始化方式是:
textView = findViewById(R.id.textView);
你必须在setcontentView
方法被调用后实例化变量,所以你必须在MainActivity2
中做如下操作:
public class MainActivity2 extends AppCompatActivity
TextView textView;
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView = findViewById(R.id.textView);// Change made here in this line
Intent intent = getIntent();
String name = intent.getStringExtra(MainActivity.EXTRA_NAME);
textView.setText("your name is "+ name)
【讨论】:
【参考方案2】:MainActivity2 类中的 TextView 文本视图未初始化。在 MainActivity2 类中使用 textView = findViewById(R.id.textView);
而不是 textView.findViewById(R.id.textView);
。
【讨论】:
以上是关于在android中使用多屏时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
Android:使用相机意图时应用程序在 onActivityResult 上崩溃
启动 Unity 时应用程序崩溃 - 在 Android Studio 中测试时不会崩溃
在android中使用firebase电话身份验证时应用程序崩溃