I have made an application with only one layout and one MainActivity. Furthermore, I added two buttons in my XML file: one is an ImageButton and other one is just a Button. I have described my buttons in my onCreate() function, and created a ClickListener for each of them. While I did not get a compiler error, when I ran the program I got the message:
"'unfortunately your app has stopped responding."
What is the problem here?
MainActivity code:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TesbihMainActivity extends Activity {
TextView display;
Button zikirbutton, resetter;
int counter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tesbih_main);
counter = 0;
zikirbutton =(Button)findViewById(R.id.allahbutton);
display =(TextView)findViewById(R.id.displayscreen);
resetter = (Button)findViewById(R.id.resetbutton);
zikirbutton .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter ++;
display.setText(""+counter);
}
});
resetter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter = 0;
display.setText(""+counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.tesbih_main, menu);
return true;
}
}
XML Code:
<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"
tools:context=".TesbihMainActivity" >
<ImageView
android:id="@+id/backroundimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="@drawable/tesbihbackround"
/>
<TextView
android:id="@+id/displayscreen"
android:layout_width="fill_parent"
android:layout_height="63dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#e9ebec"
android:text=" 0 "
android:gravity="center"
android:textSize="52dp"
android:textColor="#25404d"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="30dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="@+id/allahbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/resetbutton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:src="@drawable/imagebutton" />
<Button
android:id="@+id/resetbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/displayscreen"
android:layout_alignParentBottom="true"
android:layout_marginBottom="18dp"
android:text="Reset" />
Logcat:
02-19 23:00:26.450: D/dalvikvm(874): GC_FOR_ALLOC freed 50K, 4% free 3160K/3280K, paused 38ms, total 41ms 02-19 23:00:26.500: D/AndroidRuntime(874): Shutting down VM 02-19 23:00:26.500: W/dalvikvm(874): threadid=1: thread exiting with uncaught exception (group=0xb4a5aba8) 02-19 23:00:26.530: E/AndroidRuntime(874): FATAL EXCEPTION: main 02-19 23:00:26.530: E/AndroidRuntime(874): Process: com.tesbih, PID: 874 02-19 23:00:26.530: E/AndroidRuntime(874): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tesbih/com.tesbih.TesbihMainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.ActivityThread.access$800(ActivityThread .java:135) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.os.Handler.dispatchMessage(Handler.java:102) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.os.Looper.loop(Looper.java:136) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-19 23:00:26.530: E/AndroidRuntime(874): at java.lang.reflect.Method.invokeNative(Native Method) 02-19 23:00:26.530: E/AndroidRuntime(874): at java.lang.reflect.Method.invoke(Method.java:515) 02-19 23:00:26.530: E/AndroidRuntime(874): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-19 23:00:26.530: E/AndroidRuntime(874): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-19 23:00:26.530: E/AndroidRuntime(874): at dalvik.system.NativeStart.main(Native Method) 02-19 23:00:26.530: E/AndroidRuntime(874): Caused by: java.lang.Cl assCastException: android.widget.ImageButton cannot be cast to android.widget.Button 02-19 23:00:26.530: E/AndroidRuntime(874): at com.tesbih.TesbihMainActivity.onCreate(TesbihMainActivity.java:25) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.Activity.performCreate(Activity.java:5231) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 02-19 23:00:26.530: E/AndroidRuntime(874): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 02-19 23:00:26.530: E/AndroidRuntime(874): ... 11 more 02-19 23:00:37.200: I/Process(874): Sending signal. PID: 874 SIG: 9
You're trying to cast a Button
but it's an ImageButton
(see your XML file).
zikirbutton =(Button)findViewById(R.id.allahbutton);
should be
ImageButton zikirbutton;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
zikirbutton =(ImageButton)findViewById(R.id.allahbutton);
Note that the logcat often gives you useful informations about what didn't work and why. Here is the important line which gives you the reason and where it happened :
E/AndroidRuntime(874): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button 02-19 23:00:26.530: E/AndroidRuntime(874): at com.tesbih.TesbihMainActivity.onCreate(TesbihMainActivity.java:25)
You are getting type casting error.
Change the following thing in your code.
1) Change
Button zikirbutton;
to
ImageButton zikirbutton;
2)Change
zikirbutton =(Button)findViewById(R.id.allahbutton);
to
zikirbutton =(ImageButton)findViewById(R.id.allahbutton);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.