为什么android中的FATAL EXCEPTION的原因?

时间:2014-03-23 16:04:43

标签: android fatal-error

  

## MainActivity.java ##       ##我正在尝试使用代码查找字符串##

的字母数
package com.example.karaktersayma;

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

public class MainActivity extends Activity {

      Button karakterSay;
      EditText deger;
      TextView t;
      String s;
      int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        karakterSay=(Button)findViewById(R.id.button1);
        deger=(EditText)findViewById(R.id.editText1);
        t=(TextView)findViewById(R.id.textView1);

        karakterSay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                s=deger.getText().toString();
                 i=s.length();
                 t.setText(i);
            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
  

## activity_main.xml ##

     

      

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.karaktersayma.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
     

## manifest.xml ##

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.karaktersayma.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
## exception-fatal-error ##

03-23 17:32:53.508: E/AndroidRuntime(32104): FATAL EXCEPTION: main
03-23 17:32:53.508: E/AndroidRuntime(32104): android.content.res.Resources$NotFoundException: String resource ID #0x6
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.content.res.Resources.getText(Resources.java:231)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.widget.TextView.setText(TextView.java:4016)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at com.example.karaktersayma.MainActivity$1.onClick(MainActivity.java:34)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.view.View.performClick(View.java:4340)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.view.View$PerformClick.run(View.java:17634)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.os.Handler.handleCallback(Handler.java:725)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.os.Looper.loop(Looper.java:137)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at android.app.ActivityThread.main(ActivityThread.java:5171)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at java.lang.reflect.Method.invokeNative(Native Method)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at java.lang.reflect.Method.invoke(Method.java:511)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-23 17:32:53.508: E/AndroidRuntime(32104):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

在设置文本视图的文本时,您需要将数字转换为字符串:

t.setText(i);

需要

t.setText(String.valueOf(i));

编译器将整数读取为指向资源的指针,因此出现错误:

android.content.res.Resources$NotFoundException: String resource ID #0x6

答案 1 :(得分:0)

如果setText(int)尝试使用

,Android将始终抛出异常
t.setText(String.valueOf(i));