【问题标题】:Android Bundle - Start Activity and set a bundle to NullAndroid Bundle - 启动 Activity 并将包设置为 Null
【发布时间】:2012-08-23 17:28:59
【问题描述】:

我正在尝试启动一个包含捆绑包的活动。我正在尝试将此捆绑包设置为空,因为尚未将任何内容传递给此活动。

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 not found 错误而崩溃

编辑-添加的错误日志

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): 在 java.lang.reflect.Method.invokeNative(Native Method) 08-24 10:39:33.180: E/AndroidRuntime(21177): 在 java.lang.reflect.Method.invoke(Method.java:511) 08-24 10:39:33.180: E/AndroidRuntime(21177): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-24 10:39:33.180: E/AndroidRuntime(21177): 在 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): 在 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)

这就是解决方案,我太密集了哈哈

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

【问题讨论】:

    标签: android bundle


    【解决方案1】:

    百分比是一个整数。您对 setText(percentage) 的调用需要资源 ID,而不是百分比值。

    试试这个:setText(""+percentage)。

    这会将百分比转换为字符串并将其传递给 setText()。

    附言WOUNDEDSteveJones 也是对的,你需要调用 findViewById(),但我猜你在某个地方有这个,否则这个应用程序会以不同的方式崩溃。

    【讨论】:

    • Thnx - 关于百分比是 int,你是对的,没有看到,但它仍然崩溃,是的,我确实有 scoreA = (TextView)findViewById(R.id.counterA); 我只是没有发布整个代码,因为我很漂亮确定问题出在捆绑包上。如果我删除捆绑包的提及,则活动加载正常..
    • 我看到你在做 setText(counter_score),所以关于资源 ID 的同样问题也与那里有关。尝试 setText(""+counter_score) 代替。除此之外,我看不出你的代码有什么特别的问题。
    • 是的 - 我看到我通过输入 ("0") 解决了它这里是最终代码 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); } } 再次感谢!!
    【解决方案2】:

    您似乎从未将 scoreA 分配给该文本字段。

    您需要在引用之前添加scoreA = (TextView) findViewById(R.id.score); 之类的内容。

    【讨论】:

    • 谢谢帮助,我的错我把那部分代码留了出来..但是我引用它..scoreA = (TextView)findViewById(R.id.counterA);
    猜你喜欢
    • 2022-09-29
    • 2013-08-18
    • 2021-03-13
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2012-09-06
    相关资源
    最近更新 更多