我有一个文本视图,必须在执行时更新。此文本视图由以下xml:
定义<TextView
android:id="@+id/agi_val"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
在活动代码中,我可以访问此(和其他TextViews),如下所示:
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");
}
}
然后,在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
}
}
事情是&#34; agi_val&#34;正在查找textview,但是我以前保存过的活动的AGI文本视图,它无法访问资源&#34; agi_val&#34;其链接。错误日志如下:
///< 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)
有人知道到底发生了什么事吗?我已经有两天时间试图解决这个问题而且我不知道如何(我在互联网上搜索过,但我找不到类似的东西)。
如果有人要求提供R类ID,则与日志中第一行显示的内容相同。
答案 0 :(得分:0)
一旦调用了 setContentView(),如果您正在查看正确的布局并且该布局中存在视图,则永远不会获得空视图。
如果您通过传递错误的ID
来查找视图,则只会获得null 使用此 if (!AGI.getText().toString.matches(""))
或者
if(!TextUtils.isEmpty(AGI.getText().toString))
投放 if(AGI != null)
答案 1 :(得分:0)
我认为您不需要使用this
来引用以XML格式查找TextView
窗口小部件ID。
所以,只需这样做:
TextView AGI = (TextView) findViewById(R.id.agi_val);
您也可以通过 isEmpty() 功能检查该值是否为空。您必须检查abfToolsSaveData
而不是AGI
的空值。还要检查abfToolsSaveData
的类型。如果是integer
或double
,则必须将integer/double
值解析为string
。
if(!abfToolsSaveData.isEmpty()){
AGI.setText("Some Text");
//AGI.setText(String.valueOf("some integer content"));
}else {
}
答案 2 :(得分:0)
好吧,我在“Stack OverflowenEspañol”中问了同样的答案,我得到了答案。
问题是mc.getAGI()
返回一个int,所以使用setText(int resourceId)
的覆盖,找到具有R.string
值的资源id
。 / p>