gpt4 book ai didi

android - "Unfortunately, App name has stopped"简单应用

转载 作者:行者123 更新时间:2023-11-30 01:46:53 25 4
gpt4 key购买 nike

我开始学习 Android 应用开发。在 Android Studio gradle 中没有显示任何错误。但是在我的手机上显示“不幸的是,应用程序名称已停止”。我正在尝试通过按钮应用程序将简单的 EditTex 制作成 TextView 。我认为这很容易,但显然不是。至少有这个 fatal error 。我希望你能在这种情况下帮助我。也许这是个愚蠢的问题。

这是日志

I/art: Late-enabling -Xcheck:jni
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.winwin.kapurs.tester1, PID: 26610
E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.winwin.kapurs.tester1/com.winwin.kapurs.tester1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:2072)
E/AndroidRuntime: at com.winwin.kapurs.tester1.MainActivity.<init>(MainActivity.java:11)
E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method)
E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1606)
E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2259)
E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154) 
E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 
E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135) 
E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5292) 
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 
E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)

这是我的主要代码:

public class MainActivity extends AppCompatActivity {

final EditText mess = (EditText) findViewById(R.id.editText);
final TextView txt = (TextView) findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onClick(View v) {
if (txt.getText().toString().equals("")) {
String editTextValue = mess.getText().toString();
txt.setText(editTextValue);
} else {
txt.setText("");
mess.setText("");
}
}
}

最佳答案

移动这些初始化(保留变量声明)

final EditText mess = (EditText) findViewById(R.id.editText);
final TextView txt = (TextView) findViewById(R.id.textView);

onCreate(),这样就不会因为activity window还没有初始化而NPE,在setContentView()之后,返回值可以是非-空。

关于android - "Unfortunately, App name has stopped"简单应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com