【问题标题】:Changing the order of a button and a textview crashes my app更改按钮和文本视图的顺序使我的应用程序崩溃
【发布时间】:2012-02-01 20:58:46
【问题描述】:

我是 Android 开发新手,遇到了这个奇怪的问题。根据我的布局顺序,应用程序可以完美运行,或者只是在打开之前崩溃。

我的java代码:

package com.exmple.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.View;

public class HelloAndroid extends Activity{
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            // According to logcat. Below is the error line
    Button myButton = (Button) findViewById(R.id.button1);
    myButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                        Toast.makeText(HelloAndroid.this, "ImageButton clicked!", Toast.LENGTH_SHORT).show();
                }
    });
}
}

还有布局 XML:

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

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text1" /> 

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text1" />
</LinearLayout>

还有字符串 XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="text1">This is the first text!</string>
<string name="text2">This is the second text!</string>
<string name="app_name">Hello, Android</string>

</resources>

问题:如果我将 textview 移到按钮上方,程序会崩溃。我不明白为什么会这样。

那么:我是智障还是有什么问题?

============================== 我已经测试了问题,是的,有问题,这里是 lgocat

02-02 02:33:00.433: E/AndroidRuntime(2729): FATAL EXCEPTION: main
02-02 02:33:00.433: E/AndroidRuntime(2729): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.android.testing/com.my.android.testing.HelloAndroid}: java.lang.ClassCastException: android.widget.TextView
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.os.Looper.loop(Looper.java:130)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at java.lang.reflect.Method.invokeNative(Native Method)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at java.lang.reflect.Method.invoke(Method.java:507)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at dalvik.system.NativeStart.main(Native Method)
02-02 02:33:00.433: E/AndroidRuntime(2729): Caused by: java.lang.ClassCastException: android.widget.TextView
02-02 02:33:00.433: E/AndroidRuntime(2729):     at com.my.android.testing.TestAndActivity.onCreate(HelloAndroid.java:16)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-02 02:33:00.433: E/AndroidRuntime(2729):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-02 02:33:00.433: E/AndroidRuntime(2729):     ... 11 more

【问题讨论】:

  • 清理整个项目并重新构建!希望它会修复
  • 请分享你的logcat。当应用程序崩溃时。
  • 嗯,清洁和重建工作!

标签: android android-layout button textview


【解决方案1】:

清理您的项目将解决您的问题。

【讨论】:

    【解决方案2】:

    我运行你的代码,你是对的,我给出的例外是我们将 TextView 移到按钮之前。这很奇怪。但我发现这实际上是一些身份问题。当我在将 TextView 向上移动并开始工作后更改 Id 时。但我没有理由这样做。

    可能是某个击球手知道的。但是您也可以通过链接视图的 id 来尝试。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/main_id_text_my"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text2" />
    
        <Button
            android:id="@+id/main_id_btn_my"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text1" />
    
    </LinearLayout>
    

    在活动代码中也是如此。

    Button myButton = (Button) findViewById(R.id.main_id_btn_my);
            myButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                                Toast.makeText(TestAndActivity.this, "ImageButton clicked!", Toast.LENGTH_SHORT).show();
                        }
            });
    

    别忘了清理和构建你的项目

    【讨论】:

      【解决方案3】:

      在移动 TextView 的位置时需要小心。如果您在将 TextView 移动到其他位置后不更改它的 ID,它可能会导致您的应用崩溃。

      我不知道为什么会发生这种情况,但是更改 id 可以解决这个崩溃问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多