-1

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)?
MostafaUTD
  • 29
  • 6

2 Answers2

3

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Can I regenerating R.java without clean layout ? How ? – MostafaUTD Jul 27 '16 at 01:52
  • I went in R.java file and remove IDs and ctrl+s to save. An error occure and IDs returns and **all problems gone** and its works . Thanks for your useful helps. – MostafaUTD Jul 27 '16 at 02:16
  • @MostafaUTD In this case, you have to mark this answer as accepted one .. also you can give it an upvote `;-)` – Shafizadeh Jul 27 '16 at 02:17
  • @cricket_007 Yes, That error say the same thing and discard my manual changes. After that "Unfortunately MyApp has stopped" gone! – MostafaUTD Jul 27 '16 at 02:43
0

I basically just create a new xml file when this occurs. Sometimes rebuilding doesn't work as it is supposed to. So try creating a new xml file with the layout that you want, then you clean and rebuild.

Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49