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;
}
}
LinearLayoutin your XML layout that you are casting to aButtonwhen you usefindViewById()in your code. Double check if the view id is right. But, anyway, you should show yourActivitycode.