我正在寫一個簡單的應用程序,它將「是」設置爲這樣一個TextView (默認值爲「否」)當我點擊一個按鈕(Button是在TextView旁邊)。一切都好..它完美地工作。當我切換元素的位置時,爲什麼我的應用程序停止工作?
因此有什麼問題?當我關閉程序,然後切換(包括按鈕和TextView)的位置(通過拖動),然後令人驚訝的是該應用程序不再工作。注意到當我再次切換它們的位置(如第一次)時,它也可以工作。
任何人都可以請任何人解釋我爲什麼我的應用程序停止工作,當我切換這兩個元素的地方?
使用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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
都不行版本:
<?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="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
的Java代碼:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class FirstAppActivity extends Activity {
Button btn;
TextView txt;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
txt.setText("yes");
}
});
}
}
我嘗試在同一代碼和操作Android Studio。並獲得低於該顯示java.lang.ClassCastException錯誤:當您切換順序沒有再生R.java
android.support.v7.widget.AppCompatTextView不能轉換到android.widget.Button
FATAL EXCEPTION: main
Process: com.example.dysaniazzz.testdemo, PID: 30870
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dysaniazzz.testdemo/com.example.dysaniazzz.testdemo.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
at android.app.ActivityThread.access$800(ActivityThread.java:136)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1219)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5032)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.Button
at com.example.dysaniazzz.testdemo.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5310)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2179)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)?
at android.app.ActivityThread.access$800(ActivityThread.java:136)?
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1219)?
at android.os.Handler.dispatchMessage(Handler.java:102)?
at android.os.Looper.loop(Looper.java:136)?
at android.app.ActivityThread.main(ActivityThread.java:5032)?
at java.lang.reflect.Method.invokeNative(Native Method)?
at java.lang.reflect.Method.invoke(Method.java:515)?
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)?
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)?
at dalvik.system.NativeStart.main(Native Method)?
發佈你的完整堆棧跟蹤 –
@GabeSechan你是什麼意思「全堆棧跟蹤」? – Shafizadeh
當你崩潰時(當你看到那個不幸...屏幕時發生了什麼),一個堆棧跟蹤報告問題是什麼以及它在哪裏被打印到日誌中。你用它來調試你的問題。 –