MostafaUTD MostafaUTD - 4 months ago 35
Java Question

Why does my app stop working when I switch the place of elements?

enter image description hereI'm writing a simple application which sets "yes" to such a TextView (its value is "no" by default) when I click on a button (the Button is next to the TextView). All fine .. It works perfectly.

So what's the problem? When I close the program and then switch the place of them (both the button and the TextView) in the layout (by dragging), then surprisingly the app doesn't work anymore. Noted that when I switch the place of them again (like the first time), it works as well.

Can please anyone explain my why my app stops working when I switch the place of those two elements?

Working XML codes:

<?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>


Won't work version:

<?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 codes:

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");

}
});
}
}


I try the same codes and operation on Android Studio. And get the errors below Which shows java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to 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)?

Answer

When you switch the order without regenerating R.java, you get misleading errors because your view IDs have not been reassigned.

You must Clean & Rebuild the project. I believe Android Studio / Gradle regenerates the resources for every build, Eclipse mostly just compiles the Java code.