2017-09-20 4 views
-1

Ich habe eine Textansicht, die zur Ausführungszeit aktualisiert werden muss. Dieser Textansicht durch die folgende XML definiert ist:Android-Textansicht nicht während der Ausführungszeit gefunden

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

Im Aktivitätscode, acces i zu diesem (und anderen Textviews) wie folgt aus:

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"); 
     } 
    } 

Und dann, 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 
     } 
    } 

die Sache ist, dass die „agi_val“ Textview gefunden wird, aber die AGI Textview der Tätigkeit, dass ich vorher gerettet hatte, kann es nicht die Ressource „agi_val“, deren acces verlinkt sind. Das Fehlerprotokoll ist das folgende:

///< 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)  

Jemand weiß, was zur Hölle geschieht? Ich habe zwei Tage versucht, das zu lösen, und ich weiß nicht, wie (ich habe im Internet gesucht, aber ich kann etwas ähnliches nicht finden).

Wenn jemand nach der R-Klassen-ID fragt, entspricht dies der Anzeige in der ersten Zeile des Protokolls.

+0

versuchen ** sauber ** und ** ** Wiederaufbau Projekt –

+0

Ich habe das fast zehnmal getan –

Antwort

0

Nun, habe ich diese gleiche Antwort gefragt in „Stack-Überlauf en Español“ und ich bekomme die Antwort.

Die Sache ist, dass die mc.getAGI() einen int zurückgibt, so verwendet die Überschreibung von setText(int resourceId), die die Ressource R.string findet, die den ID-Wert hat.

Link zur Antwort: https://es.stackoverflow.com/a/103616/6568

0

Sobald setContentView() aufgerufen wurde, erhalten Sie nie eine Nullansicht, sofern Sie im richtigen Layout suchen und die Ansicht in diesem Layout vorhanden ist.

Sie erhalten nur eine Null, wenn Sie für eine Ansicht, indem man das falsche ID

Verwendung diesesif (!AGI.getText().toString.matches(""))

oder diese

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

insted suchenif(AGI != null)

+0

Vielen Dank für das, das das Problem nicht löst, aber es ist nützlich, um eine neue Sache zu wissen! –

+0

am meisten willkommen @SliferDragon, wenn es Ihnen hilft, kann meine ans annehmen –

+0

Der Code funktioniert nicht mit Ihrer Antwort ... –

0

Ich denke, Sie müssen this nicht verwenden, um auf TextView Widget ID in XML zu verweisen. So

, tun einfach:

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

Sie können prüfen, der Wert leer ist oder nicht über isEmpty() Funktion zu. Sie müssen den Nullwert abfToolsSaveData nicht überprüfen. Überprüfen Sie auch den Typ abfToolsSaveData. Wenn es integer oder double ist, dann müssen Sie den integer/double Wert zu string analysieren.

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

}else { 

} 
Verwandte Themen