代码之家  ›  专栏  ›  技术社区  ›  user3452425

为什么在android中出现致命异常?

  •  1
  • user3452425  · 技术社区  · 10 年前

    ##主要活动.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;
        }
    
    }
    

    ##活动_最小.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 回复  |  直到 10 年前
        1
  •  3
  •   eazimmerman    10 年前

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

    t.setText(i);
    

    需要

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

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

    android.content.res.Resources$NotFoundException: String resource ID #0x6
    
        2
  •  0
  •   s3nzoM    10 年前

    如果您 setText(int) 尝试使用

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