2

I do have a short Android app I am buiding using Eclipse ADT plugin that have two activities. The main activity have a button that when clicked, calls the second screen that displays my name. However, my application stops working on running it. The error I get is:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button

How can I solve this error?

Here is my main activity code:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button batta=(Button)findViewById(R.id.btnICT3631);
    batta.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            //my code goes here
            startActivity(new Intent(MainActivity.this, ICT3631.class));
        }
    }); //end but1



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
3
  • Please post your code Commented Apr 4, 2015 at 21:25
  • You probably have a LinearLayout in your XML layout that you are casting to a Button when you use findViewById() in your code. Double check if the view id is right. But, anyway, you should show your Activity code. Commented Apr 4, 2015 at 21:31
  • I have posted my main activity code guys. Commented Apr 4, 2015 at 22:05

2 Answers 2

1

This exception means that you are using button in your xml file while casting it to the linear layout in your class file. Please post your code such that I can give you a better explanation.

Sign up to request clarification or add additional context in comments.

2 Comments

Your class file code is completely fine, try changing the id of your button in xml file as well as class file. This may help you.
Cant believe it worked now. So I have button defined twice both in LinearLayout and Button view. Deleted the button id in LinearLayout and only left it defined in Button view. Issue fixed Thank you.
0

In case you experience the error

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.Button

Even if you try change layout the problem will still persist. As I got this answer helpful. I thought I could say it simple for other guys to understand. All you need do is to make sure that in your main activity layout xml file you dont have the view id that the error state you trying to cast defined twice. In my case, I have the button view id defined both in the layout level and on the view level too. So deleting the view id declaration in the layot fixed the issue and only have defined in that specific view - in my case the button view id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.