Hi I am getting this message android.widget.imagebutton cannot be cast to android.widget.button (See attachment pic for logcat error) which I don't understand why? because my code seems to be right.
Code:
ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
Calendar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent OpenCalendarbtn = new Intent (Menu.this, DisplayCalendar.class);
startActivity (OpenCalendarbtn);
}
});
Complete Layout xml file:
`
<ImageButton
android:id="#+id/Read"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/Write"
android:layout_marginRight="31dp"
android:src="#drawable/read" />
<ImageView
android:id="#+id/AppLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/logo" />
<ImageButton
android:id="#+id/Write"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/AppLogo"
android:layout_marginLeft="35dp"
android:layout_marginTop="14dp"
android:src="#drawable/write" />
<ImageButton
android:id="#+id/Calendar"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignLeft="#+id/Write"
android:layout_below="#+id/Write"
android:layout_marginTop="20dp"
android:src="#drawable/calendar/>
`
Make sure you have used the <ImageButton> tag instead of <Button> tag in your layout resource file.
That could be the cause of the error.
Change your
ImageButton Calendar = (ImageButton) findViewById (R.id.Calendar);
To:
Button Calendar = (Button) findViewById (R.id.Calendar);
Its an Eclipse problem not in your code.
Solution for this is just copy your code in xml by Ctrl+c and save the xml then again paste it again then save it and run.....this works with me.
Related
In my android application list view there is a image. The imageview is on another layout. i want to change that image to another one through code . when i run the program with the code its getting error..'
Can anyone help me to find the error thank youu ./
I have tried..
Bitmap bImag = BitmapFactory.decodeResource(this.getResources(), R.drawable.tick);
down.setImageBitmap(bImag);
and this too
myImgView.setBackgroundResource(R.drawable.monkey);
My xml code .. (I used Onclick on the imageview)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center"
android:text="asdas"
android:layout_weight="0.9"
android:paddingTop="5dp"/>
<ImageView
android:id="#+id/icon"
android:onClick="download"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="right"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/download" />
</LinearLayout>
Java code ..
public void download(View view) {
String value = getIntent().getExtras().getString("id");
if (value.equals("Pretham (2016)")) {
Intent i=new Intent(Song_List.this, MyService.class);
startService(i);
Toast.makeText(Song_List.this, "Downloading..........", Toast.LENGTH_SHORT).show();
Bitmap bImag = BitmapFactory.decodeResource(this.getResources(), R.drawable.tick);
down.setImageBitmap(bImag);
}else{
Toast.makeText(Song_List.this, "Downloaded Song.....", Toast.LENGTH_SHORT).show();
}
my logcat is..
Process: com.example.jithin.myapplication, PID: 1539
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
The imageView has:
android:onClick="download"
Only works if the layout is from Activity setContentView() not from listview or Fragment
You need to add on the adapter:
ImageView img = findViewById(R.id.download);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//CODE on click
}
});
and remove onclick from xml
I am developing Restaurant menu app. I have 9 image button in activity_main.xml. For example: If when i clicked soup image button, the app will soup price and soup image in other layout. But i dont want create 9 layouts file for this. All of prices appear one layout. I didnt write method for this. I am waiting your help. Sorry for my bad english.
This is my menu.
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton2"
android:layout_gravity="left|top"
android:background="#drawable/corbalar"
android:contentDescription="#string/corbalar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton3"
android:layout_gravity="center_horizontal|top"
android:background="#drawable/zeytinyaglilar"
android:contentDescription="#string/zeytinyaglilar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton4"
android:layout_gravity="right|top"
android:background="#drawable/spesiyaller"
android:contentDescription="#string/spesiyaller"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton5"
android:layout_gravity="left|center_vertical"
android:background="#drawable/pideler"
android:contentDescription="#string/pideler"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton6"
android:layout_gravity="center"
android:background="#drawable/salatalar"
android:contentDescription="#string/salatalar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton7"
android:layout_gravity="right|center_vertical"
android:background="#drawable/kebaplar"
android:contentDescription="#string/kebaplar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton8"
android:layout_gravity="left|bottom"
android:background="#drawable/tatlilar"
android:contentDescription="#string/tatlilar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton9"
android:layout_gravity="center_horizontal|bottom"
android:background="#drawable/mesrubatlar"
android:contentDescription="#string/mesrubatlar"
android:clickable="true"
android:onClick="degistirActivity"/>
<ImageButton
android:layout_width="130dp"
android:layout_height="170dp"
android:id="#+id/imageButton10"
android:layout_gravity="right|bottom"
android:background="#drawable/sicakicecekler"
android:contentDescription="#string/sicakicecekler"
android:clickable="true"
android:onClick="degistirActivity"/>
This is the place I'll show information.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="tr.edu.fatih.nad0.com.kilislimenu.fiyatGoster">
//my items image
<ImageButton
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageButton11"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/gosterilecekyemek" />
//my items price
<TextView
android:layout_width="200dp"
android:layout_height="50dp"
android:id="#+id/textView"
android:layout_below="#+id/imageButton11"
android:layout_centerHorizontal="true" />
What you'll want to do is use an intent with extras to pass information of the clicked item to the activity. There's a couple of ways to do this:
1) Manually create an OnClickListener for each item (ideally, you'd generate your items from a SQLite database rather than having a static list).
Using an OnClickListener would look something like this:
ImageButton myButton = (ImageButton) findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent priceIntent = new Intent(getApplicationContext(), priceActivity.class);
priceIntent.putExtra("price",6.99);
priceIntent.putExtra("name","Hamburger");
startActivity(priceIntent);
}
});
Then in your onCreate method for priceActivity.class, you'd read the values like this:
Bundle extras = getIntent().getExtras();
double price = extras.getDouble("price");
String name = extras.getString("name");
2) Use the onClick attribute in your XML to process the information through an identifying tag or some such.
In your XML you can specify android:onClick="myMethod" as well as an identifying tag, like android:tag="hamburger". Then you'd make your myMethod:
public void myMethod(View v) {
String tagId = v.getTag().toString();
// Create intent with extras like above
}
Again, ideally you'd be loading all of this information from a database so that you can easily access the information with fewer methods rather than manually entering the hard-coded data straight into the app.
you can follow below link to workout with your problem.
http://developer.android.com/training/basics/firstapp/starting-activity.html
Alternatively,
you can implement method like this on your button.
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
On another Activity, you can receive Intent and Display your price.
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
I'm writing an app for my android device that contains three buttons at the bottom of the screen. In my onCreate method I am setting three Button objects to their corresponding views using findViewById(). Here's the code I have written:
public class MainActivity extends AppCompatActivity {
Button btnAdd = null;
Button btnEdit = null;
Button btnDelete = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = (Button) findViewById(R.id.btnAdd);
btnEdit = (Button) findViewById(R.id.btnEdit);
btnDelete = (Button) findViewById(R.id.btnDelete);
init(getApplicationContext());
btnAdd.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
EventWriter.writeToFile(eventFile, getApplicationContext());
}
});
}
...
}
My XML looks like the following, with the button elements declared within a RelativeLayout:
<Button
android:id="#+id/btnDelete"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/scrollView1"
android:layout_alignRight="#+id/scrollView1"
android:layout_alignParentBottom="true"
android:text="#string/txtBtnDelete" />
<Button
android:id="#+id/btnAdd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/scrollView1"
android:layout_alignLeft="#+id/scrollView1"
android:layout_alignParentBottom="true"
android:text="#string/txtBtnAdd" />
<Button
android:id="#+id/btnEdit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/txtBtnEdit" />
When I try to run the application using the emulator in Eclipse, I get the following LogCat error at line #36 (btnDelete = (Button) findViewById(R.id.btnDelete);):
Caused by: java.lang.ClassCastException: android.widget.TableLayout cannot be cast to android.widget.Button
I do have a TableLayout earlier in the code (located inside a ScrollView) but that does not contain the buttons:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="#+id/actvSearchEvent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/actvSearchEvent"
android:layout_above="#+id/btnAdd"
android:layout_marginTop="32dp"
android:background="#drawable/scrollview_border" >
<TableLayout
android:id="#+id/tlTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
I don't see where it's getting that I'm trying to cast the TableLayout to a Button. Anybody have any ideas?
Try doing a clean and clean build folder, then run the build again.
The error means that you are still pointing to the layout with the scroll view and the table layout. And there are no buttons in it.
You can easily fix this by pasting your buttons inside your table layout, e.g.
<TableLayout...>
<Button
android:id="#+id/btnDelete"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/scrollView1"
android:layout_alignRight="#+id/scrollView1"
android:layout_alignParentBottom="true"
android:text="#string/txtBtnDelete" />
<Button
android:id="#+id/btnAdd"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/scrollView1"
android:layout_alignLeft="#+id/scrollView1"
android:layout_alignParentBottom="true"
android:text="#string/txtBtnAdd" />
<Button
android:id="#+id/btnEdit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/txtBtnEdit" />
</TableLayout>
In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ?
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:textColor="#ffffff"
/>
public void selectit(View v)
{
Log.d("tv0","ok");
if(v.getId()==tv1.getId())
{Log.d("tv1","ok");
selectoption(1);
Log.d("tv1","ok");
}
if(v.getId()==tv2.getId())
{Log.d("tv2","ok");
selectoption(2);
}
if(v.getId()==tv3.getId())
{Log.d("tv3","ok");
selectoption(3);
}
}
Add this into your xml
android:clickable="true"
You probably need to have this code in your xml
android:clickable="true"
Or Set the Clicklistenner to TextView via code by
TextView btn=(TextView) findViewById(R.id.tv1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectit(v);
}
});
It will automatically make it clickable so no need of android:clickable="true"
You can set the click handler in xml with these attribute:
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:clickable="true"
android:textColor="#ffffff"
/>
i hope it will help u all the best
Try like below:
android:onClick="onClick"
android:clickable="true"
My textview:
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
Main Activity:
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}
I have some issues using a BootStrapButton onclickListener. Here is my code. When I run the app it crashes.
BootstrapButton btn2=(BootstrapButton) findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent nextscreen=new Intent(getActivity().getApplicationContext(),Mellipage.class);
startActivity(nextscreen);
}
});
If anyone knows how I can fix it, please share.
by adding this code my problem solved!
<com.beardedhen.androidbootstrap.BootstrapButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Success"
bootstrapbutton:bb_icon_right="fa-android"
bootstrapbutton:bb_type="success" />
From your log:
java.lang.ClassCastException: android.widget.Button cannot be cast to com.beardedhen.androidbootstrap.BootstrapButton
you have probably declared your button in your layout as a Button instead of BootstrapButton
Change this
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp" />
to
<com.beardedhen.androidbootstrap.BootstrapButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Success"
bootstrapbutton:bb_icon_right="fa-android"
bootstrapbutton:bb_type="success" />