单击按钮后应用程序停止工作

时间:2014-11-13 00:21:19

标签: java android eclipse

我是Java编程的新手。大约一两个星期前开始。有图灵和Visual Basic的一些经验。请尽量保持解释尽可能简单。对不起,如果帖子有点乱,第一次在这里发帖。

这是我的src java文件

    package com.example.pokethedot;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class PokeTheDot extends ActionBarActivity {

        int counter; 
        Button add;
        TextView display;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_poke_the_dot);
            counter=0;
            add=(Button) findViewById(R.id.bAdd);
            display=(TextView) findViewById(R.id.tvDisplay);
            add.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    counter++;
                    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.poke_the_dot, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }

这是我的xml

<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="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.thenewboston.travis.StartingPoint" 
    android:background="@drawable/background1"
    >

<TextView
    android:id="@+id/tvDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:gravity="center"
    android:text="@string/base_count"
    android:textColor="#FFFFFF"
    android:textSize="45sp" />

<Button
    android:id="@+id/bAdd"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:layout_centerInParent="true"
    android:background="@drawable/thingy"
    android:minHeight="40dp"
    android:minWidth="40dp" />

</RelativeLayout>

这是我的Android清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.pokethedot"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ptd"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:logo="@drawable/ptd">
        <activity
            android:name=".PokeTheDot"
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的logcat

11-12 19:08:04.780: D/dalvikvm(698): GC_FOR_ALLOC freed 99K, 3% free 8153K/8327K, paused 34ms, total 36ms
11-12 19:08:04.780: I/dalvikvm-heap(698): Grow heap (frag case) to 8.800MB for 829456-byte allocation
11-12 19:08:04.840: D/dalvikvm(698): GC_CONCURRENT freed 1K, 3% free 8962K/9159K, paused 29ms+3ms, total 61ms
11-12 19:08:05.050: D/gralloc_goldfish(698): Emulator without GPU emulation detected.
11-12 19:09:10.250: W/ResourceType(698): No package identifier when getting value for resource number 0x00000001
11-12 19:09:10.250: D/AndroidRuntime(698): Shutting down VM
11-12 19:09:10.250: W/dalvikvm(698): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-12 19:09:10.260: E/AndroidRuntime(698): FATAL EXCEPTION: main
11-12 19:09:10.260: E/AndroidRuntime(698): android.content.res.Resources$NotFoundException: String resource ID #0x1
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.content.res.Resources.getText(Resources.java:229)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.widget.TextView.setText(TextView.java:3620)
11-12 19:09:10.260: E/AndroidRuntime(698):  at com.example.pokethedot.PokeTheDot$1.onClick(PokeTheDot.java:29)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.view.View.performClick(View.java:4084)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.view.View$PerformClick.run(View.java:16966)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.os.Handler.handleCallback(Handler.java:615)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.os.Looper.loop(Looper.java:137)
11-12 19:09:10.260: E/AndroidRuntime(698):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 19:09:10.260: E/AndroidRuntime(698):  at java.lang.reflect.Method.invokeNative(Native Method)
11-12 19:09:10.260: E/AndroidRuntime(698):  at java.lang.reflect.Method.invoke(Method.java:511)
11-12 19:09:10.260: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 19:09:10.260: E/AndroidRuntime(698):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 19:09:10.260: E/AndroidRuntime(698):  at dalvik.system.NativeStart.main(Native Method)
11-12 19:09:23.040: E/Trace(723): error opening trace file: No such file or directory (2)
11-12 19:09:23.560: D/dalvikvm(723): GC_FOR_ALLOC freed 102K, 3% free 8153K/8327K, paused 30ms, total 31ms
11-12 19:09:23.560: I/dalvikvm-heap(723): Grow heap (frag case) to 8.800MB for 829456-byte allocation
11-12 19:09:23.610: D/dalvikvm(723): GC_CONCURRENT freed 1K, 3% free 8962K/9159K, paused 21ms+3ms, total 50ms
11-12 19:09:23.790: D/gralloc_goldfish(723): Emulator without GPU emulation detected.
11-12 19:09:24.860: W/ResourceType(723): No package identifier when getting value for resource number 0x00000001
11-12 19:09:24.860: D/AndroidRuntime(723): Shutting down VM
11-12 19:09:24.860: W/dalvikvm(723): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-12 19:09:24.870: E/AndroidRuntime(723): FATAL EXCEPTION: main
11-12 19:09:24.870: E/AndroidRuntime(723): android.content.res.Resources$NotFoundException: String resource ID #0x1
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.content.res.Resources.getText(Resources.java:229)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.widget.TextView.setText(TextView.java:3620)
11-12 19:09:24.870: E/AndroidRuntime(723):  at com.example.pokethedot.PokeTheDot$1.onClick(PokeTheDot.java:29)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.view.View.performClick(View.java:4084)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.view.View$PerformClick.run(View.java:16966)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.os.Handler.handleCallback(Handler.java:615)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.os.Looper.loop(Looper.java:137)
11-12 19:09:24.870: E/AndroidRuntime(723):  at android.app.ActivityThread.main(ActivityThread.java:4745)
11-12 19:09:24.870: E/AndroidRuntime(723):  at java.lang.reflect.Method.invokeNative(Native Method)
11-12 19:09:24.870: E/AndroidRuntime(723):  at java.lang.reflect.Method.invoke(Method.java:511)
11-12 19:09:24.870: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-12 19:09:24.870: E/AndroidRuntime(723):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-12 19:09:24.870: E/AndroidRuntime(723):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

试试这个:

display.setText(String.valueOf(counter));

使用int作为setText的参数意味着您要使用字符串资源,在这种情况下,标识为#0x1的资源不存在。

答案 1 :(得分:0)

制作

int couter;
Button add;

最后,

final int counter;
final Button add;

并更改

display.setText(counter);

作为

display.setText(new String(counter));

这可能会解决您的问题。您必须在匿名类中使用最终变量。