• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android app crashes when I add a button to the layout xml file

HeavensSentSword

Well-Known Member
So I have been having this issues for two days now and don't know why this is happening. When I try to update the layout xml file for a activity by just adding a button it crashes the class. When I delete the button it works fine. The button doesn't even have to do anything just be there and it will crash.

So by adding this to the xml layout file it causes it to break
HTML:
<Button
        android:id="@+id/Test"
        android:text="test"
        android:layout_below="@+id/nameTag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

I just add this in the userinfo Relative Layout holder right under the logout button

This will then give this error

Code:
03-08 15:04:46.031 11983-11983/     E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: , PID: 11983
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{CustomerPage}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ListView
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ListView
                                                                                   at apivita.fusion.co.jp.apivitajapan.CustomerPage.onCreate(CustomerPage.java:70)
                                                                                   at android.app.Activity.performCreate(Activity.java:6237)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                   at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)


I don't understand why it is crashing. If I just delete the button then the code works fine and does what it should.

This is the Layout that I am working on
HTML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#000000">

<RelativeLayout
    android:layout_alignParentTop="true"
    android:id="@+id/userinfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/nameTag"
        android:text="Customer Name:"
        android:textSize="18dp"
        android:textColor="#ffffff"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/customerName"
        android:textSize="18dp"
        android:textColor="#ff8bea"
        android:layout_toRightOf="@+id/nameTag"
        />
    <Button
        android:id="@+id/account"
        android:text="Account"
        android:layout_below="@+id/Logout"
        android:layout_alignParentRight="true"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:textSize="8dp"
        />
    <Button
        android:id="@+id/Logout"
        android:text="Logout"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:textSize="8dp"
        />


</RelativeLayout>
<RelativeLayout
    android:id="@+id/pointinfo"
    android:background="#e6ffdb"
    android:layout_below="@+id/userinfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/basehoneycomb"
        android:id="@+id/honeycomb"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:layout_centerInParent="true"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bee"
        android:src="@drawable/bee"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/honeycomb"
        android:layout_centerInParent="true"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pointText"
        android:layout_below="@+id/honeycomb"
        android:textColor="#ff8bea"
        android:text="Points: "
        android:textSize="20dp"
        android:layout_marginTop="15dp"
        />

</RelativeLayout>
<RelativeLayout
    android:id="@+id/hisotryinfo"
    android:layout_below="@id/pointinfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/historyLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="History"
        android:textColor="#ffffff"
        android:textSize="16dp"
        />
    <ListView
        android:layout_marginTop="1dp"
        android:layout_below="@+id/historyLabel"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/history"
        android:textColor="#ff8bea"
        >
    </ListView>
    <ListView
        android:layout_marginTop="1dp"
        android:layout_below="@+id/historyLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/products"
        android:textColor="#ff8bea"
        android:layout_toRightOf="@+id/history"
        android:layout_toEndOf="@+id/history">
    </ListView>

</RelativeLayout>
<RelativeLayout
    android:id="@+id/barcodeinfo"
    android:background="#ffffff"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_below="@id/hisotryinfo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/barcode"
        android:layout_centerHorizontal="true">
    </ImageView>

    <TextView
        android:id="@+id/number"
        android:layout_below="@+id/barcode"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:textAlignment="center"
        android:layout_height="wrap_content"
        android:textColor="#000000"

        />

</RelativeLayout>


And this is the activity that is using it.
Java:
public class CustomerPage  extends AppCompatActivity {

//Needed varialbe

//"Customer name", X
// "Purchase History",
// "Point Balance", X
// "Customer Barcode X
// and id number" X

/*
*Point structure
* Each JPY 100 = 1 mile (JPY 3,000 = 30 miles)
Over JPY 8,000 = *1.5 time (e.g. JPY 8,000 = 120 miles)
1,000 miles = JPY 1,000 yen worth discount
8th day ever month = W point day
*/
String name;
//barcode variables
String prefix = "12345";
String customerNumber = "6789012454751";
Bitmap barcode = null;

int pointBalance = 700;

ListView historyListView, productListView;
final String strJson = "{ \"Purchase\" :[{\"item\":\"Product 1\",\"date\":\"01/11/2016\"}, {\"item\":\"Product 2\",\"date\":\"12/24/2016\"}] }";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customer_page);
    Bundle extras = getIntent().getExtras();
    if(extras !=null)
    {
        name = extras.getString("user_name");
    }
    ArrayList<String> date = new ArrayList<String>();
    ArrayList<String> items =  new ArrayList<String>();
    //get the data for the history
    CustomerJsonParser cjp = new CustomerJsonParser();
    cjp.parsesData(strJson, date, items);

    historyListView = (ListView)findViewById(R.id.history);
    //build purchase history
    ArrayAdapter ad = new ArrayAdapter(this, R.layout.custom_listview, date);
    historyListView.setAdapter(ad);

    productListView = (ListView)findViewById(R.id.products);
    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.custom_listview, items);
    productListView.setAdapter(adapter);

    //set up the User interface
    ImageView imageview = (ImageView)findViewById(R.id.barcode);
    TextView num = (TextView)findViewById(R.id.number);
    TextView pointText = (TextView)findViewById(R.id.pointText);
    TextView customerName = (TextView)findViewById(R.id.customerName);
    Button logout = (Button)findViewById(R.id.Logout);

    //Set up the customer information in the UI
    pointText.setText(Integer.toString(pointBalance));
    customerName.setText(name);
    //create barcode
    String barcodeNumber = prefix + customerNumber;
    Log.d("Barcode ", barcodeNumber);
    try{
        barcode = BarcodeGenerator.encodeAsBitmap(barcodeNumber, BarcodeFormat.ITF, 1000, 100);
        imageview.setImageBitmap(barcode);
        num.setText(barcodeNumber);

    }catch (WriterException e)
    {
        Log.d("Barcode Generation"," Failed to create");
    }

    logout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //when logging out reset the Perference values to defualt values
            String resetPass = "PASSWORD_NEW";
            String resetUser = "USER_NAME";
            CustomerPerferences.writeString(getApplicationContext(), CustomerPerferences.USER_NAME, resetUser);
            CustomerPerferences.writeString(getApplicationContext(), CustomerPerferences.PASSWORD, resetPass);
            Log.d("Loging in", "Go!");
            Intent goToLogin = new Intent(v.getContext(),LoginPage.class);
            startActivity(goToLogin);
        }
    });
 
Look at this line in the error:
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ListView

In your java, try to
import android.widget.AdapterView;
import android.widget.ListView;

UPDATE
By the way, what are you listing in your ListView?
R u using an ArrayAdapter to connect to a string-array in the XML?
 
Last edited:
That's a strange one. BTW can you just clarify what is line 70 of CustomerPage, because that's where the exception is happening?
The runtime has presumably resolved that resource id to a TextView instead of the expected ListView. Although why that should be affected by the addition of a button is not clear.

Have you tried a clean build? You may have an old 'R' file which has some incorrect references to resource ids. I have found in the past that can happen, and results in the system getting confused when you have clashing ids.
 
That's great. Yeah sometimes Studio gets in a tangle by leaving old stuff hanging around, particularly when you've renamed (and probably reused) the same names for different things.
 
Back
Top Bottom