递增int时,我的简单Android应用程序崩溃了

时间:2012-11-07 19:31:49

标签: android button

当我按下按钮时,我的应用程序总是崩溃。我是android的新手,你能帮助我吗?

当用户按下按钮时,它应该增加一个int,然后在textView中显示它。

activity_main.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="match_parent"
    android:orientation="horizontal" >

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/button_send" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/text"
        android:textSize="30sp"
        tools:context=".DisplayMessageActivity" />

    <!-- android:visibility="invisible" -->

    <TextView
        android:id="@+id/val"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:text="            " />

</RelativeLayout>

MainActivity.java

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
    public final static String  EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    public int mult = 1;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /** Called when the user clicks on the Send button */
    public void sendMessage(View view)
    {
        //Intent intent = new Intent(this, DisplayMessageActivity.class);
        //TextView text = (TextView) findViewById(R.id.text);

        TextView val = (TextView) findViewById(R.id.val);
        val.setText(mult);
        mult+=1;
        //EditText editText = (EditText) findViewById(R.id.edit_message);
        //String message = editText.getText().toString();
        //intent.putExtra(EXTRA_MESSAGE, message);
        //startActivity(intent);
    }
}

这是崩溃日志

11-07 14:23:05.663: I/Choreographer(1126): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-07 14:23:05.898: D/gralloc_goldfish(1126): Emulator without GPU emulation detected.
11-07 14:23:10.543: I/Choreographer(1126): Skipped 51 frames!  The application may be doing too much work on its main thread.
11-07 14:23:11.076: W/ResourceType(1126): No package identifier when getting value for resource number 0x00000001
11-07 14:23:11.083: D/AndroidRuntime(1126): Shutting down VM
11-07 14:23:11.083: W/dalvikvm(1126): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-07 14:23:11.263: E/AndroidRuntime(1126): FATAL EXCEPTION: main
11-07 14:23:11.263: E/AndroidRuntime(1126): java.lang.IllegalStateException: Could not execute method of the activity
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View$1.onClick(View.java:3591)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View.performClick(View.java:4084)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View$PerformClick.run(View.java:16966)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.os.Handler.handleCallback(Handler.java:615)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.os.Looper.loop(Looper.java:137)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at dalvik.system.NativeStart.main(Native Method)
11-07 14:23:11.263: E/AndroidRuntime(1126): Caused by: java.lang.reflect.InvocationTargetException
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.view.View$1.onClick(View.java:3586)
11-07 14:23:11.263: E/AndroidRuntime(1126):     ... 11 more
11-07 14:23:11.263: E/AndroidRuntime(1126): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.content.res.Resources.getText(Resources.java:229)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at android.widget.TextView.setText(TextView.java:3620)
11-07 14:23:11.263: E/AndroidRuntime(1126):     at com.example.myfirstapp.MainActivity.sendMessage(MainActivity.java:34)
11-07 14:23:11.263: E/AndroidRuntime(1126):     ... 14 more
11-07 14:23:15.013: I/Process(1126): Sending signal. PID: 1126 SIG: 9

2 个答案:

答案 0 :(得分:2)

您无法将文本视图的内容设置为int。相反,你必须先将其改为字符串

替换

val.setText(mult);

val.setText(Integer.toString(mult));

答案 1 :(得分:1)

我相信如果传入int,setText方法会查找资源。在使用setText之前,需要将变量mult更改为String。以下任何一种都可以使用:

val.setText("" + mult);

或者

val.setText(Integer.toString(mult));