Question

## MainActivity.java ## ## I am trying a code to find number of letters of a string ##

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)
Was it helpful?

Solution

You need to cast your number to a string when setting the text of your text view:

t.setText(i);

needs to be

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

The compiler reads the integer as a pointer to a resource, hence the error:

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

OTHER TIPS

Android will always throw an Exception if you setText(int) try using

t.setText(String.valueOf(i));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top