0

I have an AsyncTask, I want to set Text of TextView in my layout.xml file, but my Application is Crashed with Error this

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

kindly help me!

public class UploadVideo extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
    
        
    super.onCreate(savedInstanceState);
    
        transferUtility = Util.getTransferUtility(this);
    
        requestWindowFeature(getWindow().FEATURE_NO_TITLE);
    
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    
        setContentView(R.layout.my_activity);
    
    
        context = this;
    
    
        new get_people_data().execute();
    }


    public class get_people_data extends AsyncTask<String,String,String>{
    
    
        final String TAG = "get_people_data";
    
    
        String API_URL = "http://api.expressionsapp.com/users/auth0_5732cc152781665b7b5dfee6/status/";
    
        int front;
    
        int behind;
    
        @Override
    
        protected String doInBackground(String...arg0)
        {
    
            try
    
            {
    
                JsonParser jParser = new JsonParser();
    
                JSONObject json = jParser.getJSONFromUrl(API_URL);
    
                behind= json.getInt("countBack");
    
                front= json.getInt("countFront");
    
    
            }catch (JSONException e)
            {
    
                e.printStackTrace();
    
            }
    
            return null;
    
        }
    
    
        @Override
    
        protected void onPostExecute(String s)
        {
    
            TextView ppl_infront;
    
            TextView ppl_behind;
    
            ppl_infront = (TextView) findViewById(R.id.ppl_infront);
    
            ppl_infront.setText(front);
    
            ppl_behind = (TextView) findViewById(R.id.ppl_behind);
    
            ppl_behind.setText(behind);
    
        }
    
    }
}
Ruban J
  • 622
  • 1
  • 7
  • 31

3 Answers3

2

The problem is on

ppl_infront.setText(front);
 
ppl_behind.setText(behind);

because you are passing and int(front is of type int) using

final void setText(int resid)

and the system search in the R file with that int id (that is a file generated automatically)

Change with this:

ppl_infront.setText(String.valueOf(front));
      
ppl_behind.setText(String.vaueOf(behind));

using

final void setText(CharSequence text)

here the official reference

appersiano
  • 2,670
  • 22
  • 42
0

Do your

ppl_infront = (TextView) findViewById(R.id.ppl_infront);


and

ppl_behind = (TextView) findViewById(R.id.ppl_behind);


in onCreate() method below your setContentView(R.layout.my_activity);


If it does not solve your issue, post your xml file.

Sahana Prabhakar
  • 581
  • 1
  • 8
  • 21
  • If he uses this, the textview will have to be instance variable. Why is this needed? – Shaishav Aug 02 '16 at 09:16
  • It is not about keeping it global. If he does it in `onPostExecute()` the context for `findViewById()` will be the async task context. Hence it may crash. – Sahana Prabhakar Aug 02 '16 at 09:21
  • I doubt that man. Since, `findViewById` is being run in `Activity` context only. There is no `findViewById` in `AsyncTask` so it just cannot take its context. – Shaishav Aug 02 '16 at 09:23
  • If you can see his `onPostExecute()` method, it contains `findViewById()` for the textView. – Sahana Prabhakar Aug 02 '16 at 09:29
  • I'm saying the `onPostExecute()` method in Android doesn't have a `findViewById()`. So, the code above is running in Activity context only which does have that method since it compiles and run properly. – Shaishav Aug 02 '16 at 09:31
0

quick solution :

if you don't want to mess with String.value(front), you can set your text as follow.

ppl_behind.setText(front + "");
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
  • quick solution but very bad programming suggestion! Please do not follow that bad practise – appersiano Aug 02 '16 at 09:33
  • what is bad in that mate? if you can explain, may be it will help me. – V-rund Puro-hit Aug 02 '16 at 09:35
  • Ok,the code work but is all question of legibility and clean code. The code MUST explain itself. concatenate an empty string is just a workaround. – appersiano Aug 02 '16 at 09:44
  • well.. for me what works is worth. I believe that your code is inappropriate as log as it's affecting your app performance. but i am glad you pointed out. – V-rund Puro-hit Aug 02 '16 at 09:49
  • I do not think so, read this interesting [blog](https://dzone.com/articles/stringvalueofint-vs-int) and this [SO post](http://stackoverflow.com/questions/7752347/string-valueof-vs-concatenation-with-empty-string) ...you will change your mind! – appersiano Aug 02 '16 at 10:13
  • it's matter of 0.014 microseconds. it's totally depends upon developer and how many time he gonna use this. anyways thanks for the article. it really helped. and just for the record you should edit your comment. link in broken i guess. – V-rund Puro-hit Aug 02 '16 at 10:24