Resources$NotFoundException: String resource ID #0x0

1

I am trying to design an app to generate a random number from 0 to the value set by user on seekbar. The code is getting executed but when I set seekBar value and click button,the app crashes I'm using android studio This is the code:

package com.example.vedantsapp;

import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
Button Roll;
 TextView res;
 TextView Hm;
 SeekBar s1;

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

        Roll=(Button)findViewById(R.id.rollButton);

        res = (TextView)findViewById(R.id.resultsTestView);
        Hm = (TextView)findViewById(R.id.textView);
        s1 = (SeekBar)findViewById(R.id.seekBar);


        Roll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int x=s1.getProgress();
                Random rand=new Random();
                int r = rand.nextInt(x - 0 +1)+0;
                res.setText(r);
            }
        });
}
}

Error Log: These are the error logs(logcat-->Error)

2020-03-19 18:22:16.180 30212-30212/? E/mple.vedantsap: Unknown bits set in runtime_flags: 0x8000
2020-03-19 18:22:16.270 30212-30242/? E/Perf: Fail to get file list com.example.vedantsapp
2020-03-19 18:22:16.270 30212-30242/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-03-19 18:22:16.270 30212-30242/? E/Perf: Fail to get file list com.example.vedantsapp
2020-03-19 18:22:16.270 30212-30242/? E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-03-19 18:22:25.633 30212-30212/? E/mple.vedantsap: Invalid ID 0x00000000.
2020-03-19 18:22:25.634 30212-30212/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.vedantsapp, PID: 30212
    android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:367)
        at android.widget.TextView.setText(TextView.java:6402)
        at com.example.vedantsapp.MainActivity$1.onClick(MainActivity.java:36)
        at android.view.View.performClick(View.java:7201)
        at android.view.View.performClickInternal(View.java:7170)
        at android.view.View.access$3500(View.java:806)
        at android.view.View$PerformClick.run(View.java:27562)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)



java
android
asked on Stack Overflow Mar 19, 2020 by CharlieGone • edited Mar 19, 2020 by EpicPandaForce

1 Answer

1
int r = rand.nextInt(x - 0 +1)+0;
res.setText(r);

Here you're calling setText() that takes an int argument. The int is expected to be a resource identifier. There's no resource to be found with that random number.

Use setText() that takes a String instead:

res.setText(String.valueOf(r));
answered on Stack Overflow Mar 19, 2020 by laalto

User contributions licensed under CC BY-SA 3.0