我正在尝试启动一个包含Bundles的Activity。我正试图将此捆绑包设置为null,因为尚未传递给此活动。
public class DailyActivities extends Activity implements OnClickListener{
TextView scoreA
int gotA;
int counter_score;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
counter_score=0;
int questions_1 = 26;
initialize();
///////PSEUDO CODE...this is where Im trying to say, if no bundled is passed,
////////then setText for scoreA to the int counter score,
////////so that the activity doesn't crash due to null pointer exception////
Bundle gotA = getIntent().getExtras();
if(gotA == null){
scoreA.setText(counter_score);
}else if (gotA != null){
gotLetterA = gotA.getInt("key");
counter_score = gotLetterA;
int percentage = (int)( gotLetterA * 100.0 / questions_1 + 0.5);
scoreA.setText(percentage);
}
}
当前活动因未找到android.resources错误而崩溃
编辑 - 添加错误日志
08-24 10:39:33.180:E / AndroidRuntime(21177):致命异常:主要 08-24 10:39:33.180:E / AndroidRuntime(21177):java.lang.RuntimeException:无法启动活动ComponentInfo {com.test.app/com.test.app.DailyActivities}:android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.ActivityThread.access $ 600(ActivityThread.java:130) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.os.Handler.dispatchMessage(Handler.java:99) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.os.Looper.loop(Looper.java:137) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.ActivityThread.main(ActivityThread.java:4745) 08-24 10:39:33.180:E / AndroidRuntime(21177):at java.lang.reflect.Method.invokeNative(Native Method) 08-24 10:39:33.180:E / AndroidRuntime(21177):at java.lang.reflect.Method.invoke(Method.java:511) 08-24 10:39:33.180:E / AndroidRuntime(21177):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 08-24 10:39:33.180:E / AndroidRuntime(21177):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-24 10:39:33.180:E / AndroidRuntime(21177):at dalvik.system.NativeStart.main(Native Method) 08-24 10:39:33.180:E / AndroidRuntime(21177):引起:android.content.res.Resources $ NotFoundException:字符串资源ID#0x0 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.content.res.Resources.getText(Resources.java:229) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.widget.TextView.setText(TextView.java:3620) 08-24 10:39:33.180:E / AndroidRuntime(21177):at com.MovilTeacher_titan.app.DailyActivities.onCreate(DailyActivities.java:164) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.Activity.performCreate(Activity.java:5008) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 08-24 10:39:33.180:E / AndroidRuntime(21177):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
Soooooo在这里是解决方案,我是密集的lol
Bundle gotA = getIntent().getExtras();{
if(gotA == null){ scoreA.setText("0%");
}else {
myPkg = gotA.getInt("key");
counter_score = myPkg;
int percentage = (int)( myPkg * 100.0 / questions_1 + 0.5);
scoreA.setText(""+percentage);
}
}
答案 0 :(得分:1)
百分比是一个int。您对setText(百分比)的调用期望是资源ID,而不是百分比值。
试试这个:setText(“”+ percent)。
这会将百分比转换为字符串,并将其传递给setText()。
P.S。 WOUNDEDSteveJones也是对的,你需要调用findViewById(),但我猜你在某处或者这个应用程序会以不同的方式崩溃。
答案 1 :(得分:0)
看起来您永远不会将scoreA分配给该文本字段。
在引用之前,您需要添加scoreA = (TextView) findViewById(R.id.score);
之类的内容。