java - setText 导致应用程序崩溃

标签 java android android-layout

我是 Android 开发新手。我不明白为什么这会在模拟器中崩溃,当我在 Android 模拟器上运行这个程序时,它打开然后快速关闭,给我一条“不幸的是, Hello World 已停止”消息。

            public class HelloAndroidActivity extends Activity {
                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    TextView t=(TextView)findViewById(R.id.textView1);  
                    t.setText("Secoknd text to display!"); 

                }   
            }

xml 文件:

            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal" >





                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="match_parent"
                    android:layout_height="38dp"
                    android:editable="true"
                    android:text="TextView" />

            </LinearLayout>

日志猫:

            10-20 20:07:48.784: D/AndroidRuntime(601): Shutting down VM
            10-20 20:07:48.784: W/dalvikvm(601): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
            10-20 20:07:48.804: E/AndroidRuntime(601): FATAL EXCEPTION: main
            10-20 20:07:48.804: E/AndroidRuntime(601): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.helloandroid/com.example.helloandroid.HelloAndroidActivity}: java.lang.NullPointerException
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.os.Handler.dispatchMessage(Handler.java:99)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.os.Looper.loop(Looper.java:137)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.ActivityThread.main(ActivityThread.java:4340)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at java.lang.reflect.Method.invokeNative(Native Method)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at java.lang.reflect.Method.invoke(Method.java:511)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at dalvik.system.NativeStart.main(Native Method)
            10-20 20:07:48.804: E/AndroidRuntime(601): Caused by: java.lang.NullPointerException
            10-20 20:07:48.804: E/AndroidRuntime(601):  at com.example.helloandroid.HelloAndroidActivity.onCreate(HelloAndroidActivity.java:16)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.Activity.performCreate(Activity.java:4465)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
            10-20 20:07:48.804: E/AndroidRuntime(601):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
            10-20 20:07:48.804: E/AndroidRuntime(601):  ... 11 more
            10-20 20:12:48.894: I/Process(601): Sending signal. PID: 601 SIG: 9

最佳答案

如果您提供的代码中 TextView 为 null,则您必须设置错误的布局 ID。您的代码示例具有 setContentView(R.layout.main);。您提供的 xml 是否可能来自 main.xml 以外的文件?

关于java - setText 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7841631/

相关文章:

android - 在 Horizo​​ntalScrollView 内部缩放 ImageView

android - 长按时的圆形按钮背景和提示

JAVA 按钮对齐

java - 从 char 到 int : why (int)givenString. 的转换 charAt(2) 给出 48 with givenString = "11001"?

java - request.getUserPrincipal() 为空

Android 删除进度对话框焦点并使其下的 gridview 可聚焦?

java - okhttp大文件上传失败

android - 如何在单击按钮时打开内置的开放式谷歌地图

android - Android AudioRecord 的 AudioHardwareALSA 错误

android - 如何解决 sdk 17 和 18 之间列表项布局行为变化的变化?