【发布时间】: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