不幸的是应用程序已停止工作 [英] Unfortunately app has stopped working
问题描述
我是新的android应用开发。我在做这个教程app.It是一个非常简单的问题。它增加了一个,减去一个来自counter.When我在模拟器中运行它,它说:不幸的是教程已停止工作。有在code没有错误。 API级别是17。请帮助我。
code的Java:
公共类起点延伸活动{
INT计数器= 0;
按钮加,减;
TextView中显示; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.startingpoint);
添加=(按钮)findViewById(R.id.bAdd);
减去=(按钮)findViewById(R.id.bSubtract);
显示=(按钮)findViewById(R.id.text);
add.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
反++;
display.setText(以下简称总为+计数器);
}
}); subtract.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(视图v){
计数器 - ;
display.setText(以下简称总为+计数器);
} });
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
。getMenuInflater()膨胀(R.menu.startingpoint,菜单);
返回true;
}
}
布局code在XML:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> < TextView的机器人:ID =@ + ID /文
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=你总为0
机器人:TEXTSIZE =35dp
机器人:layout_gravity =中心
机器人:比重=中心/> <按钮的android:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=添加一个
机器人:layout_gravity =中心
机器人:TEXTSIZE =25dp
机器人:ID =@ + ID / BADD/> <按钮的android:layout_width =250dp
机器人:layout_height =WRAP_CONTENT
机器人:文字=减一
机器人:layout_gravity =中心
机器人:TEXTSIZE =25dp
机器人:ID =@ + ID / bSubtract/>
< / LinearLayout中>
下面是logcat的:
2月3日至2日:45:10.730:D / AndroidRuntime(780):关闭VM
二月3日至2日:45:10.730:W / dalvikvm(780):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a71930)
二月3日至2日:45:10.750:E / AndroidRuntime(780):致命异常:主要
二月3日至2日:45:10.750:E / AndroidRuntime(780):了java.lang.RuntimeException:无法启动活动ComponentInfo {tutorial.example.tutorial / tutorial.example.tutorial.Startingpoint}:java.lang.ClassCastException:机器人.widget.TextView不能转换为android.widget.Button
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.os.Handler.dispatchMessage(Handler.java:99)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.os.Looper.loop(Looper.java:137)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.ActivityThread.main(ActivityThread.java:5041)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在java.lang.reflect.Method.invokeNative(本机方法)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在java.lang.reflect.Method.invoke(Method.java:511)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在dalvik.system.NativeStart.main(本机方法)
二月3日至2日:45:10.750:E / AndroidRuntime(780):java.lang.ClassCastException:致android.widget.TextView不能转换为android.widget.Button
二月3日至2日:45:10.750:E / AndroidRuntime(780):在tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.Activity.performCreate(Activity.java:5104)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
二月3日至2日:45:10.750:E / AndroidRuntime(780):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
二月3日至2日:45:10.750:E / AndroidRuntime(780):11 ...更多
显示=(按钮)findViewById(R.id.text);
应
显示=(的TextView)findViewById(R.id.text);
由于显示
应该引用的TextView
实例,但你显式铸造成按钮
,这些都是不可以兼容的类型。
I am new to android application development. I was doing this tutorial app.It's a very simple one. It adds one and subtracts one from the counter.When I run it in the emulator ,it says "Unfortunately tutorial has stopped working." There are no errors in the code. The API level is 17. Please help me out.
Code for java:
public class Startingpoint extends Activity {
int counter=0;
Button add,subtract;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startingpoint);
add = (Button) findViewById(R.id.bAdd);
subtract= (Button) findViewById(R.id.bSubtract);
display= (Button) findViewById(R.id.text);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
display.setText("The total is " + counter);
}
});
subtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
display.setText("The total is " + counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.startingpoint, menu);
return true;
}
}
Layout code in xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="35dp"
android:layout_gravity="center"
android:gravity="center"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="@+id/bAdd"/>
<Button android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Subtract One"
android:layout_gravity="center"
android:textSize="25dp"
android:id="@+id/bSubtract"/>
</LinearLayout>
Here is the logcat :
03-02 02:45:10.730: D/AndroidRuntime(780): Shutting down VM
03-02 02:45:10.730: W/dalvikvm(780): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-02 02:45:10.750: E/AndroidRuntime(780): FATAL EXCEPTION: main
03-02 02:45:10.750: E/AndroidRuntime(780): java.lang.RuntimeException: Unable to start activity ComponentInfo{tutorial.example.tutorial/tutorial.example.tutorial.Startingpoint}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.os.Looper.loop(Looper.java:137)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 02:45:10.750: E/AndroidRuntime(780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 02:45:10.750: E/AndroidRuntime(780): at dalvik.system.NativeStart.main(Native Method)
03-02 02:45:10.750: E/AndroidRuntime(780): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
03-02 02:45:10.750: E/AndroidRuntime(780): at tutorial.example.tutorial.Startingpoint.onCreate(Startingpoint.java:22)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Activity.performCreate(Activity.java:5104)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-02 02:45:10.750: E/AndroidRuntime(780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-02 02:45:10.750: E/AndroidRuntime(780): ... 11 more
display= (Button) findViewById(R.id.text);
should be
display= (TextView) findViewById(R.id.text);
Since display
is supposed to reference a TextView
instance, but you're explictly casting into a Button
, and these are not compatible types.
这篇关于不幸的是应用程序已停止工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!