gpt4 book ai didi

Android:应用程序在开始新 Activity 时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:21 24 4
gpt4 key购买 nike

基本上,我想启动一个打印出输入字符串和下面的图像的 Activity 。

在 activity_display_message.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:text="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>

<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/serious" />


</LinearLayout>

这是 OnCreate() 中的代码:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_display_message);
Intent intent = getIntent();

String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

ImageView image2 = (ImageView) findViewById(R.id.imageView2);


// Create the text view

TextView textView = (TextView)findViewById(R.id.textView);
//textView.setTextSize(20);

textView.setText(message);
// Set the text view as the activity layout


// Show the Up button in the action bar.
setupActionBar();
}

这段代码有什么问题吗?

编辑:不确定如何获取堆栈跟踪,但我刚刚在 Catlog 中找到了这些行:

07-05 12:26:53.677: E/AppsCustomizePagedView(611): Widget ComponentInfo{com.th.android.widget.gTabsimiClock/com.th.android.widget.gTabsimiClock.GTabsimiClock5x1} can not fit on this device (736, 144)
07-05 12:26:53.692: E/AppsCustomizePagedView(611): Widget ComponentInfo{at.abraxas.powerwidget.free/at.abraxas.powerwidget.free.SettingsAppWidgetProvider_5} can not fit on this device (760, 80)
07-05 12:26:57.958: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:26:58.146: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:26:58.630: E/AudioCache(131): Error 1, -1004 occurred
07-05 12:27:07.833: E/AudioCache(131): Heap size overflow! req size 1052672, max size: 1048576

这是堆栈跟踪吗?

07-05 12:41:10.058: E/TAG(22474): STACKTRACE
07-05 12:41:10.058: E/TAG(22474): java.lang.NullPointerException
07-05 12:41:10.058: E/TAG(22474): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:38)
07-05 12:41:10.058: E/TAG(22474): at android.app.Activity.performCreate(Activity.java:5104)
07-05 12:41:10.058: E/TAG(22474): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 12:41:10.058: E/TAG(22474): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:41:10.058: E/TAG(22474): at android.os.Looper.loop(Looper.java:137)
07-05 12:41:10.058: E/TAG(22474): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 12:41:10.058: E/TAG(22474): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:41:10.058: E/TAG(22474): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 12:41:10.058: E/TAG(22474): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 12:41:10.058: E/TAG(22474): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 12:41:10.058: E/TAG(22474): at dalvik.system.NativeStart.main(Native Method)

运行时崩溃时的堆栈跟踪:(上面的是通过执行 try/catch 获得的)

07-05 12:52:24.386: E/AndroidRuntime(25496): FATAL EXCEPTION: main
07-05 12:52:24.386: E/AndroidRuntime(25496): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.os.Looper.loop(Looper.java:137)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-05 12:52:24.386: E/AndroidRuntime(25496): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 12:52:24.386: E/AndroidRuntime(25496): at java.lang.reflect.Method.invoke(Method.java:511)
07-05 12:52:24.386: E/AndroidRuntime(25496): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-05 12:52:24.386: E/AndroidRuntime(25496): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-05 12:52:24.386: E/AndroidRuntime(25496): at dalvik.system.NativeStart.main(Native Method)
07-05 12:52:24.386: E/AndroidRuntime(25496): Caused by: java.lang.NullPointerException
07-05 12:52:24.386: E/AndroidRuntime(25496): at com.example.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:38)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.Activity.performCreate(Activity.java:5104)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-05 12:52:24.386: E/AndroidRuntime(25496): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-05 12:52:24.386: E/AndroidRuntime(25496): ... 11 more

消息不为空。当我将 OnCreate 更改为以下时,将打印消息:(当然,不是图像)

// Create the text view

//TextView textView = (TextView)findViewById(R.id.textView);
TextView textView = new TextView(this);
textView.setTextSize(20);

textView.setText(message);
setContentView(textView);

回答:发现错误:android:text="@+id/textView"。应该是android:id,不是android:text ...

但是谢谢大家的帮助。你们让像我这样的初学者的 Android 编程体验变得更好了。

最佳答案

需要先给activity设置内容。

您可以将当前 View 层次结构的findViewById 设置为 Activity 。由于您尚未将内容设置到 Activity,因此如果您尝试初始化 View ,您将得到 NullPointerException

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setTextSize(20);
textView.setText(message);
....
}

编辑:

正如 Squonk 评论的那样 setText(CharSequence text) 检查 null

来自

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.2_r1/android/widget/TextView.java#TextView.setText%28java.lang.CharSequence%2Candroid.widget.TextView.BufferType%29

实现看起来像

3561    private void setText(CharSequence text, BufferType type,
3562 boolean notifyBefore, int oldlen) {
3563 if (text == null) { // checking for null
3564 text = "";
3565 }
.....

所以第 38 行是 textView.setText(message) 导致 NPE。所以 textView 没有初始化导致 NPE。

进一步更新和 Squonk 评论

textview 可以为空,因为指定了错误(或无效)的 R.id 或者在某些情况下如果 R.java文件已损坏。在后一种情况下,使用 Project -> Clean 应该可以解决问题。

还有这个

android:text="@+id/textView" // this is actually setting text to textview.

你不见了

android:id="@+id/textView" // textview id attribute missing

所以当你尝试初始化时

TextView textView = (TextView)findViewById(R.id.textView);
// there is no textview with id textView which caused NPE

关于Android:应用程序在开始新 Activity 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17495445/

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