Android text view not found during execution time

-1

I have a text view that has to be updated on execution time. This text view is defined by the following xml:

<TextView
android:id="@+id/agi_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

In the activity code, i acces to this (and other TextViews) like this:

private TextView AGI;
///< Some stuff

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_abf__char_gen__basic_info);

        abfToolsSaveData = (ABFToolsSaveData) getIntent().getParcelableExtra("SaveDataClass");

        this.AGI = (TextView) findViewById(R.id.agi_val);
        ///< Some stuff

        if (abfToolsSaveData != null) {
            this.chargeData(true);
        } else {
            Log.d("INFO", "abfToolsSaveData is null");
        }
    }

And then, in chargeData(boolean):

public void chargeData(boolean firstTime){
        if(firstTime){
            MainCharacteristics mc = abfToolsSaveData.getCharacter().getMainCharacteristics(); ///< We get the main characteristics, auto-generated the first time

            /**
             * Set the Text Views with the default value
             */
            if(AGI != null){    ///< I've done this to check if the text view is null or not
                Log.d("INFO", String.valueOf(AGI));
                AGI.setText(mc.getAGI());
            }
            else
                Log.d("INFO","AGI IS NULL");
            ///< Some stuff
        }
    }

The thing is that the "agi_val" textview is being found, but the AGI textview of the activity, that i had saved before, it can't acces the resource "agi_val" whose linked. The error log is the following:

///< This first line shows the value of the "agi_val" ID. As you can see, it's not null.
    09-20 10:07:32.344 1266-1266/com.noeselmastersonlosdados.sliferdragon.penandpapercompanion D/INFO: android.support.v7.widget.AppCompatTextView{b670993 V.ED.... ......ID 0,0-0,0 #7f0d0077 app:id/agi_val}
    09-20 10:07:32.344 1266-1266/com.noeselmastersonlosdados.sliferdragon.penandpapercompanion W/ResourceType: No package identifier when getting value for resource number 0x00000009
    --------- beginning of crash
    09-20 10:07:32.347 1266-1266/com.noeselmastersonlosdados.sliferdragon.penandpapercompanion E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.noeselmastersonlosdados.sliferdragon.penandpapercompanion, PID: 1266
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noeselmastersonlosdados.sliferdragon.penandpapercompanion/com.noeselmastersonlosdados.sliferdragon.penandpapercompanion.ABF_CharGen_BasicInfo}: android.content.res.Resources$NotFoundException: String resource ID #0x9
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
    at android.app.ActivityThread.access$800(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
  Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x9
    at android.content.res.Resources.getText(Resources.java:299)
    at android.widget.TextView.setText(TextView.java:4132)
///< This line is the assigment of the text
    at com.noeselmastersonlosdados.sliferdragon.penandpapercompanion.ABF_CharGen_BasicInfo.chargeData(ABF_CharGen_BasicInfo.java:73)
    at com.noeselmastersonlosdados.sliferdragon.penandpapercompanion.ABF_CharGen_BasicInfo.onCreate(ABF_CharGen_BasicInfo.java:54)
    at android.app.Activity.performCreate(Activity.java:5990)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Someone knows what the hell is happening? I've been two days trying to solve this and I don't know how (i've searched on internet, but i can't find something similar).

If someone asks for the R class ID, its the same as show on the first line in the log.

android
android-resources
asked on Stack Overflow Sep 20, 2017 by Slifer Dragon

3 Answers

0

Once setContentView() has been called, you will never get a null View provided you are looking in the correct layout and the View exists in that layout.

You will only get a null if you're looking for a View by passing the wrong ID

use this if (!AGI.getText().toString.matches(""))

or this

if(!TextUtils.isEmpty(AGI.getText().toString))

insted of if(AGI != null)

answered on Stack Overflow Sep 20, 2017 by AskNilesh • edited Sep 20, 2017 by AskNilesh
0

I think you don't need to use this to reference to find TextView widget id in XML.

So, simply do this:

TextView AGI = (TextView) findViewById(R.id.agi_val);

You can check the value is empty or not via isEmpty() function too. You have to check null value of abfToolsSaveData not AGI. Check the type of abfToolsSaveData also. If it integer or double, then you have to parse the integer/double value to string.

if(!abfToolsSaveData.isEmpty()){
    AGI.setText("Some Text");
    //AGI.setText(String.valueOf("some integer content"));

}else {

}
answered on Stack Overflow Sep 20, 2017 by Satan Pandeya
0

Well, i've asked this same answer in "Stack Overflow en Español" and i get the answer.

The thing is that the mc.getAGI() is returning an int, so is using the override of setText(int resourceId), that finds the resource R.string that has the id value.

Link to the answer: https://es.stackoverflow.com/a/103616/6568

answered on Stack Overflow Sep 20, 2017 by Slifer Dragon • edited Jan 9, 2019 by AskNilesh

User contributions licensed under CC BY-SA 3.0