1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by HeavensSentSword, Mar 8, 2016.

  1. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    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:
    1.        android:id="@+id/Test"
    2.        android:text="test"
    3.        android:layout_below="@+id/nameTag"
    4.        android:layout_width="wrap_content"
    5.        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 (Text):
    1. 03-08 15:04:46.031 11983-11983/     E/AndroidRuntime: FATAL EXCEPTION: main
    2.                                                                                Process: , PID: 11983
    3.                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{CustomerPage}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ListView
    4.                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    5.                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    6.                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    7.                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    8.                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
    9.                                                                                    at android.os.Looper.loop(Looper.java:148)
    10.                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
    11.                                                                                    at java.lang.reflect.Method.invoke(Native Method)
    12.                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    13.                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    14.                                                                                 Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ListView
    15.                                                                                    at apivita.fusion.co.jp.apivitajapan.CustomerPage.onCreate(CustomerPage.java:70)
    16.                                                                                    at android.app.Activity.performCreate(Activity.java:6237)
    17.                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    18.                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    19.                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    20.                                                                                    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    21.                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    22.                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
    23.                                                                                    at android.os.Looper.loop(Looper.java:148)
    24.                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
    25.                                                                                    at java.lang.reflect.Method.invoke(Native Method)
    26.                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    27.                                                                                    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:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:fitsSystemWindows="true"
    8. android:background="#000000">
    9.  
    10. <RelativeLayout
    11.    android:layout_alignParentTop="true"
    12.    android:id="@+id/userinfo"
    13.    android:layout_width="match_parent"
    14.    android:layout_height="wrap_content"
    15.    >
    16.     <TextView
    17.        android:layout_alignParentTop="true"
    18.        android:layout_width="wrap_content"
    19.        android:layout_height="wrap_content"
    20.        android:id="@+id/nameTag"
    21.        android:text="Customer Name:"
    22.        android:textSize="18dp"
    23.        android:textColor="#ffffff"
    24.        />
    25.  
    26.     <TextView
    27.        android:layout_width="wrap_content"
    28.        android:layout_height="wrap_content"
    29.        android:id="@+id/customerName"
    30.        android:textSize="18dp"
    31.        android:textColor="#ff8bea"
    32.        android:layout_toRightOf="@+id/nameTag"
    33.        />
    34.     <Button
    35.        android:id="@+id/account"
    36.        android:text="Account"
    37.        android:layout_below="@+id/Logout"
    38.        android:layout_alignParentRight="true"
    39.        android:layout_width="70dp"
    40.        android:layout_height="30dp"
    41.        android:textSize="8dp"
    42.        />
    43.     <Button
    44.        android:id="@+id/Logout"
    45.        android:text="Logout"
    46.        android:layout_alignParentRight="true"
    47.        android:layout_alignParentTop="true"
    48.        android:layout_width="70dp"
    49.        android:layout_height="30dp"
    50.        android:textSize="8dp"
    51.        />
    52.  
    53.  
    54. </RelativeLayout>
    55. <RelativeLayout
    56.    android:id="@+id/pointinfo"
    57.    android:background="#e6ffdb"
    58.    android:layout_below="@+id/userinfo"
    59.    android:layout_width="match_parent"
    60.    android:layout_height="wrap_content">
    61.     <ImageView
    62.        android:layout_width="wrap_content"
    63.        android:layout_height="wrap_content"
    64.        android:src="@drawable/basehoneycomb"
    65.        android:id="@+id/honeycomb"
    66.        android:layout_alignParentLeft="true"
    67.        android:layout_marginLeft="10dp"
    68.        android:layout_centerInParent="true"
    69.        />
    70.     <ImageView
    71.        android:layout_width="wrap_content"
    72.        android:layout_height="wrap_content"
    73.        android:id="@+id/bee"
    74.        android:src="@drawable/bee"
    75.        android:layout_alignParentRight="true"
    76.        android:layout_toRightOf="@+id/honeycomb"
    77.        android:layout_centerInParent="true"
    78.        />
    79.     <TextView
    80.        android:layout_width="wrap_content"
    81.        android:layout_height="wrap_content"
    82.        android:id="@+id/pointText"
    83.        android:layout_below="@+id/honeycomb"
    84.        android:textColor="#ff8bea"
    85.        android:text="Points: "
    86.        android:textSize="20dp"
    87.        android:layout_marginTop="15dp"
    88.        />
    89.  
    90. </RelativeLayout>
    91. <RelativeLayout
    92.    android:id="@+id/hisotryinfo"
    93.    android:layout_below="@id/pointinfo"
    94.    android:layout_width="match_parent"
    95.    android:layout_height="wrap_content">
    96.  
    97.     <TextView
    98.        android:id="@+id/historyLabel"
    99.        android:layout_width="wrap_content"
    100.        android:layout_height="wrap_content"
    101.        android:layout_marginTop="5dp"
    102.        android:text="History"
    103.        android:textColor="#ffffff"
    104.        android:textSize="16dp"
    105.        />
    106.     <ListView
    107.        android:layout_marginTop="1dp"
    108.        android:layout_below="@+id/historyLabel"
    109.        android:layout_width="100dp"
    110.        android:layout_height="wrap_content"
    111.        android:id="@+id/history"
    112.        android:textColor="#ff8bea"
    113.        >
    114.     </ListView>
    115.     <ListView
    116.        android:layout_marginTop="1dp"
    117.        android:layout_below="@+id/historyLabel"
    118.        android:layout_width="wrap_content"
    119.        android:layout_height="wrap_content"
    120.        android:id="@+id/products"
    121.        android:textColor="#ff8bea"
    122.        android:layout_toRightOf="@+id/history"
    123.        android:layout_toEndOf="@+id/history">
    124.     </ListView>
    125.  
    126. </RelativeLayout>
    127. <RelativeLayout
    128.    android:id="@+id/barcodeinfo"
    129.    android:background="#ffffff"
    130.    android:layout_marginLeft="20dp"
    131.    android:layout_marginRight="20dp"
    132.    android:layout_below="@id/hisotryinfo"
    133.    android:layout_width="match_parent"
    134.    android:layout_height="wrap_content">
    135.     <ImageView
    136.        android:layout_width="match_parent"
    137.        android:layout_height="wrap_content"
    138.        android:id="@+id/barcode"
    139.        android:layout_centerHorizontal="true">
    140.     </ImageView>
    141.  
    142.     <TextView
    143.        android:id="@+id/number"
    144.        android:layout_below="@+id/barcode"
    145.        android:layout_centerHorizontal="true"
    146.        android:layout_width="wrap_content"
    147.        android:textAlignment="center"
    148.        android:layout_height="wrap_content"
    149.        android:textColor="#000000"
    150.  
    151.        />
    152.  
    153. </RelativeLayout>

    And this is the activity that is using it.
    Code (Java):
    1. public class CustomerPage  extends AppCompatActivity {
    2.  
    3. //Needed varialbe
    4.  
    5. //"Customer name", X
    6. // "Purchase History",
    7. // "Point Balance", X
    8. // "Customer Barcode X
    9. // and id number" X
    10.  
    11. /*
    12. *Point structure
    13. * Each JPY 100 = 1 mile (JPY 3,000 = 30 miles)
    14. Over JPY 8,000 = *1.5 time (e.g. JPY 8,000 = 120 miles)
    15. 1,000 miles = JPY 1,000 yen worth discount
    16. 8th day ever month = W point day
    17. */
    18. String name;
    19. //barcode variables
    20. String prefix = "12345";
    21. String customerNumber = "6789012454751";
    22. Bitmap barcode = null;
    23.  
    24. int pointBalance = 700;
    25.  
    26. ListView historyListView, productListView;
    27. final String strJson = "{ \"Purchase\" :[{\"item\":\"Product 1\",\"date\":\"01/11/2016\"}, {\"item\":\"Product 2\",\"date\":\"12/24/2016\"}] }";
    28.  
    29. @Override
    30. protected void onCreate(Bundle savedInstanceState) {
    31.     super.onCreate(savedInstanceState);
    32.     setContentView(R.layout.customer_page);
    33.     Bundle extras = getIntent().getExtras();
    34.     if(extras !=null)
    35.     {
    36.         name = extras.getString("user_name");
    37.     }
    38.     ArrayList<String> date = new ArrayList<String>();
    39.     ArrayList<String> items =  new ArrayList<String>();
    40.     //get the data for the history
    41.     CustomerJsonParser cjp = new CustomerJsonParser();
    42.     cjp.parsesData(strJson, date, items);
    43.  
    44.     historyListView = (ListView)findViewById(R.id.history);
    45.     //build purchase history
    46.     ArrayAdapter ad = new ArrayAdapter(this, R.layout.custom_listview, date);
    47.     historyListView.setAdapter(ad);
    48.  
    49.     productListView = (ListView)findViewById(R.id.products);
    50.     ArrayAdapter adapter = new ArrayAdapter(this, R.layout.custom_listview, items);
    51.     productListView.setAdapter(adapter);
    52.  
    53.     //set up the User interface
    54.     ImageView imageview = (ImageView)findViewById(R.id.barcode);
    55.     TextView num = (TextView)findViewById(R.id.number);
    56.     TextView pointText = (TextView)findViewById(R.id.pointText);
    57.     TextView customerName = (TextView)findViewById(R.id.customerName);
    58.     Button logout = (Button)findViewById(R.id.Logout);
    59.  
    60.     //Set up the customer information in the UI
    61.     pointText.setText(Integer.toString(pointBalance));
    62.     customerName.setText(name);
    63.     //create barcode
    64.     String barcodeNumber = prefix + customerNumber;
    65.     Log.d("Barcode ", barcodeNumber);
    66.     try{
    67.         barcode = BarcodeGenerator.encodeAsBitmap(barcodeNumber, BarcodeFormat.ITF, 1000, 100);
    68.         imageview.setImageBitmap(barcode);
    69.         num.setText(barcodeNumber);
    70.  
    71.     }catch (WriterException e)
    72.     {
    73.         Log.d("Barcode Generation"," Failed to create");
    74.     }
    75.  
    76.     logout.setOnClickListener(new View.OnClickListener() {
    77.         @Override
    78.         public void onClick(View v) {
    79.             //when logging out reset the Perference values to defualt values
    80.             String resetPass = "PASSWORD_NEW";
    81.             String resetUser = "USER_NAME";
    82.             CustomerPerferences.writeString(getApplicationContext(), CustomerPerferences.USER_NAME, resetUser);
    83.             CustomerPerferences.writeString(getApplicationContext(), CustomerPerferences.PASSWORD, resetPass);
    84.             Log.d("Loging in", "Go!");
    85.             Intent goToLogin = new Intent(v.getContext(),LoginPage.class);
    86.             startActivity(goToLogin);
    87.         }
    88.     });
    89.  
    90.  
    91.  


     

    Advertisement

  2. ac4android

    ac4android Well-Known Member
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    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?
     
    #2 ac4android, Mar 8, 2016
    Last edited: Mar 8, 2016
    HeavensSentSword likes this.
  3. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    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.
     
    HeavensSentSword likes this.
  4. HeavensSentSword

    HeavensSentSword Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    93
    Posts:
    134
    Joined:
    Jan 19, 2016

    Jan 19, 2016
    134
    36
    93
    Male
    @LV426 I cleaned the build twice and renamed the ListView and then it worked. Thanks I appreciate it.
    @ac4android I was using this to display a arraylist of dates for logins
     
    LV426 likes this.
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    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.
     
    HeavensSentSword likes this.

Share This Page

Loading...