Passing Values from one activity to another, using Object and getting a resource not found error.

StackOverflow https://stackoverflow.com/questions/17946579

Pergunta

I have two pages in my application, I am passing one certain value from one page to another:

Class Level Definition: (From the sending class)

 BuyGold50 BG5 = null;

In onCreate() of the same class:

 BG5 = new BuyGold50();

In a certain function of the first activity--Before calling intent to second Activity

 BG5.SetPrice(Gold);

================================================================================

Code of the BuyGold50 class

public class BuyGold50 extends Activity {

    public static String B;
    static int X;

    public void SetPrice(String Price) {
        B = Price;
        X = Integer.parseInt(B);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.buy_gold50);
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText(X);
    }

}

Log Cat

07-30 11:59:07.304: E/AndroidRuntime(615): FATAL EXCEPTION: main
07-30 11:59:07.304: E/AndroidRuntime(615): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Geet/com.Geet.BuyGold50}: android.content.res.Resources$NotFoundException: String resource ID #0xb34
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.os.Looper.loop(Looper.java:123)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-30 11:59:07.304: E/AndroidRuntime(615):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 11:59:07.304: E/AndroidRuntime(615):  at java.lang.reflect.Method.invoke(Method.java:507)
07-30 11:59:07.304: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-30 11:59:07.304: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-30 11:59:07.304: E/AndroidRuntime(615):  at dalvik.system.NativeStart.main(Native Method)
07-30 11:59:07.304: E/AndroidRuntime(615): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xb34
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.content.res.Resources.getText(Resources.java:201)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.widget.TextView.setText(TextView.java:2857)
07-30 11:59:07.304: E/AndroidRuntime(615):  at com.Geet.BuyGold50.onCreate(BuyGold50.java:21)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 11:59:07.304: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-30 11:59:07.304: E/AndroidRuntime(615):  ... 11 more
Foi útil?

Solução

Change

 tv.setText(X);

with

tv.setText(String.valueOf(X));

if you pass an int as argument to setText, internally, android will look for a String with that id. If it does not exist it will throws Resources$NotFoundException

Outras dicas

You are trying to set int value in

tetview.setText() or Toast.makeText(), which it will take as string resource id.

So try to give int value in like this

.setText(""+intvalue) or Toast.makeText(context,""+intvalue,..)

The error has occurred at com.Geet.BuyGold50.onCreate(BuyGold50.java:21), see your logcat for this. This exception occurred due to the fact that a String was not found which you had passed named Gold in first activity.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top