不幸的是程序名已停止

时间:2013-11-30 01:24:56

标签: java android

所以我刚开始学习Android应用程序开发,当我玩UI时一切正常,但是当我开始添加一些Java代码时,每当我运行程序时它都会运行但是当我运行时点击它会说的按钮

  

不幸的是程序名称已停止

我的.xml看起来像

<TextView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"
    android:text="Your total is: 0"
    android:textSize="45dp" />

<Button
    android:id="@+id/subtract"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/add"
    android:layout_alignRight="@+id/add"
    android:layout_centerVertical="true"
    android:text="Subtract One" />

<Button
    android:id="@+id/add"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:layout_above="@+id/subtract"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:text="Add One" />

我的.java

package com.example.tabletprogramming;

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 MainActivity extends Activity {

    int total;
    Button add;
    Button sub;
    TextView display;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        total = 0;
        add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                total++;
                display.setText(total);

            }
        });
        sub = (Button) findViewById(R.id.subtract);
        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                total--;
                display.setText(total);
            }
        });

        display = (TextView) findViewById(R.id.display);

    }

}

我的错误看起来像这样

11-29 20:11:35.275: D/gralloc_goldfish(1000): Emulator without GPU emulation detected.
11-29 20:11:37.555: W/ResourceType(1000): No package identifier when getting value for resource number 0x00000001
11-29 20:11:37.625: D/AndroidRuntime(1000): Shutting down VM
11-29 20:11:37.625: W/dalvikvm(1000): threadid=1: thread exiting with uncaught exception (group=0xb4b05b90)
11-29 20:11:37.645: E/AndroidRuntime(1000): FATAL EXCEPTION: main
11-29 20:11:37.645: E/AndroidRuntime(1000): Process: com.example.tabletprogramming, PID: 1000
11-29 20:11:37.645: E/AndroidRuntime(1000): android.content.res.Resources$NotFoundException: String resource ID #0x1
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.content.res.Resources.getText(Resources.java:244)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.widget.TextView.setText(TextView.java:3899)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at com.example.tabletprogramming.MainActivity$1.onClick(MainActivity.java:30)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.view.View.performClick(View.java:4424)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.view.View$PerformClick.run(View.java:18383)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.os.Handler.handleCallback(Handler.java:733)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.os.Looper.loop(Looper.java:137)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at java.lang.reflect.Method.invoke(Method.java:515)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-29 20:11:37.645: E/AndroidRuntime(1000):     at dalvik.system.NativeStart.main(Native Method)
11-29 20:11:39.965: I/Process(1000): Sending signal. PID: 1000 SIG: 9
11-29 20:11:49.305: D/gralloc_goldfish(1070): Emulator without GPU emulation detected.
11-29 20:11:50.705: W/ResourceType(1070): No known package when getting value for resource number 0xffffffff
11-29 20:11:50.705: D/AndroidRuntime(1070): Shutting down VM
11-29 20:11:50.725: W/dalvikvm(1070): threadid=1: thread exiting with uncaught exception (group=0xb4b05b90)
11-29 20:11:50.775: E/AndroidRuntime(1070): FATAL EXCEPTION: main
11-29 20:11:50.775: E/AndroidRuntime(1070): Process: com.example.tabletprogramming, PID: 1070
11-29 20:11:50.775: E/AndroidRuntime(1070): android.content.res.Resources$NotFoundException: String resource ID #0xffffffff
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.content.res.Resources.getText(Resources.java:244)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.widget.TextView.setText(TextView.java:3899)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at com.example.tabletprogramming.MainActivity$2.onClick(MainActivity.java:40)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.view.View.performClick(View.java:4424)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.view.View$PerformClick.run(View.java:18383)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.os.Handler.handleCallback(Handler.java:733)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.os.Looper.loop(Looper.java:137)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at java.lang.reflect.Method.invoke(Method.java:515)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-29 20:11:50.775: E/AndroidRuntime(1070):     at dalvik.system.NativeStart.main(Native Method)
11-29 20:12:32.385: I/Process(1070): Sending signal. PID: 1070 SIG: 9
11-29 20:14:00.995: D/gralloc_goldfish(1123): Emulator without GPU emulation detected.
11-29 20:14:03.345: W/ResourceType(1123): No package identifier when getting value for resource number 0x00000001
11-29 20:14:03.345: D/AndroidRuntime(1123): Shutting down VM
11-29 20:14:03.355: W/dalvikvm(1123): threadid=1: thread exiting with uncaught exception (group=0xb4b05b90)
11-29 20:14:03.365: E/AndroidRuntime(1123): FATAL EXCEPTION: main
11-29 20:14:03.365: E/AndroidRuntime(1123): Process: com.example.tabletprogramming, PID: 1123
11-29 20:14:03.365: E/AndroidRuntime(1123): android.content.res.Resources$NotFoundException: String resource ID #0x1
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.content.res.Resources.getText(Resources.java:244)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.widget.TextView.setText(TextView.java:3899)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at com.example.tabletprogramming.MainActivity$1.onClick(MainActivity.java:37)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.view.View.performClick(View.java:4424)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.view.View$PerformClick.run(View.java:18383)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.os.Handler.handleCallback(Handler.java:733)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.os.Looper.loop(Looper.java:137)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at java.lang.reflect.Method.invoke(Method.java:515)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-29 20:14:03.365: E/AndroidRuntime(1123):     at dalvik.system.NativeStart.main(Native Method)
11-29 20:14:06.545: I/Process(1123): Sending signal. PID: 1123 SIG: 9
11-29 20:19:32.685: D/gralloc_goldfish(1173): Emulator without GPU emulation detected.
11-29 20:19:34.055: W/ResourceType(1173): No package identifier when getting value for resource number 0x00000001
11-29 20:19:34.055: D/AndroidRuntime(1173): Shutting down VM
11-29 20:19:34.055: W/dalvikvm(1173): threadid=1: thread exiting with uncaught exception (group=0xb4b05b90)
11-29 20:19:34.065: E/AndroidRuntime(1173): FATAL EXCEPTION: main
11-29 20:19:34.065: E/AndroidRuntime(1173): Process: com.example.tabletprogramming, PID: 1173
11-29 20:19:34.065: E/AndroidRuntime(1173): android.content.res.Resources$NotFoundException: String resource ID #0x1
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.content.res.Resources.getText(Resources.java:244)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.widget.TextView.setText(TextView.java:3899)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at com.example.tabletprogramming.MainActivity$1.onClick(MainActivity.java:30)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.view.View.performClick(View.java:4424)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.view.View$PerformClick.run(View.java:18383)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.os.Handler.handleCallback(Handler.java:733)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.os.Looper.loop(Looper.java:137)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at java.lang.reflect.Method.invoke(Method.java:515)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-29 20:19:34.065: E/AndroidRuntime(1173):     at dalvik.system.NativeStart.main(Native Method)
11-29 20:21:09.265: D/gralloc_goldfish(1215): Emulator without GPU emulation detected.
11-29 20:21:11.135: W/ResourceType(1215): No package identifier when getting value for resource number 0x00000001
11-29 20:21:11.135: D/AndroidRuntime(1215): Shutting down VM
11-29 20:21:11.135: W/dalvikvm(1215): threadid=1: thread exiting with uncaught exception (group=0xb4b05b90)
11-29 20:21:11.175: E/AndroidRuntime(1215): FATAL EXCEPTION: main
11-29 20:21:11.175: E/AndroidRuntime(1215): Process: com.example.tabletprogramming, PID: 1215
11-29 20:21:11.175: E/AndroidRuntime(1215): android.content.res.Resources$NotFoundException: String resource ID #0x1
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.content.res.Resources.getText(Resources.java:244)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.widget.TextView.setText(TextView.java:3899)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at com.example.tabletprogramming.MainActivity$1.onClick(MainActivity.java:30)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.view.View.performClick(View.java:4424)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.view.View$PerformClick.run(View.java:18383)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.os.Handler.handleCallback(Handler.java:733)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.os.Looper.loop(Looper.java:137)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at java.lang.reflect.Method.invokeNative(Native Method)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at java.lang.reflect.Method.invoke(Method.java:515)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-29 20:21:11.175: E/AndroidRuntime(1215):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

问题在于这一行:

display.setText(total);

您传递的是系统正在解释为资源ID的整数(总计)。试试这个:

display.setText(Integer.toString(total));

答案 1 :(得分:0)

代码中的变量total是int类型。传递给setText时,Android将此int视为不存在的资源ID并使程序崩溃。您需要将int转换为String(或者特别是实现CharSequence的类型)。

请参阅TextView文档:http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

答案 2 :(得分:0)

替换display.setText(total);display.setText(total+"");