什么样的代码我不能放入onCreate方法

时间:2014-06-30 03:03:12

标签: java android oncreate

所以我正在制作一个简单的游戏应用程序,用于计算主要活动中的分数,然后将此整数发送到新的活动。然后我在这个新的Activity中有一个文本View,它假设采用通过intent发送的整数,并将其设置为文本View的文本(所以基本上它会发送用户的分数,并在另一个Activity中显示分数)。我将这段代码放入新Activity的onCreate()中,然后它崩溃了,“ScoreText.setText(score2);” ScoreText是文本View,score2是字符串。我不知道我是否不允许在onCreate()中更改视图的属性,但似乎我不能。我也尝试将此代码放入一个方法中,并在onCreate()中启动该方法,但这也会使应用程序崩溃,因此我假设我也无法将方法放入onCreate()中。那么究竟我可以将什么放入onCreate()?并且,在开始下一个活动后如何更改文本视图的文本?

这是新活动的onCreate():

    TextView ScoreText;
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_level);


    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    Intent info=getIntent();
    int Score=info.getIntExtra("UsersScore", 0);
    String score2=""+Score;
    TextView ScoreText=(TextView)(findViewById(R.id.tvScore));
    ScoreText.setText(score2);

}

Log Cat:

06-29 23:12:40.459: D/ActivityThread(20281): setTargetHeapUtilization:0.25
06-29 23:12:40.469: D/ActivityThread(20281): setTargetHeapIdealFree:8388608
06-29 23:12:40.469: D/ActivityThread(20281): setTargetHeapConcurrentStart:2097152
06-29 23:12:40.939: I/Adreno200-EGLSUB(20281): <ConfigWindowMatch:2136>: Format RGBA_8888.
06-29 23:12:41.009: **E**/(20281): <s3dReadConfigFile:75>: Can't open file for reading
06-29 23:12:41.009: **E**/(20281): <s3dReadConfigFile:75>: Can't open file for reading
06-29 23:12:43.462: W/dalvikvm(20281): threadid=1: thread exiting with uncaught exception (group=0x41642438)
06-29 23:12:43.462: **E**/AndroidRuntime(20281): FATAL EXCEPTION: main
06-29 23:12:43.462: **E**/AndroidRuntime(20281): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thewordgame/com.example.thewordgame.LevelActivity}: java.lang.NullPointerException
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.os.Looper.loop(Looper.java:137)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.ActivityThread.main(ActivityThread.java:4953)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at java.lang.reflect.Method.invokeNative(Native Method)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at java.lang.reflect.Method.invoke(Method.java:511)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at dalvik.system.NativeStart.main(Native Method)
06-29 23:12:43.462: **E**/AndroidRuntime(20281): Caused by: java.lang.NullPointerException
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at com.example.thewordgame.LevelActivity.onCreate(LevelActivity.java:34)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.Activity.performCreate(Activity.java:5160)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    ... 11 more

2 个答案:

答案 0 :(得分:0)

你绝对可以将这种代码放在onCreate方法中。

但看起来ScoreText没有初始化。检查日志,如果可能,将它们放在这里。这将提供有关该问题的更多细节。

Caused by: java.lang.NullPointerException
06-29 23:12:43.462: **E**/AndroidRuntime(20281):    at com.example.thewordgame.LevelActivity.onCreate(LevelActivity.java:34)

LevelActivity.onCreate(LevelActivity.java:34)上的代码是什么(第34行)?

答案 1 :(得分:0)

您应该检查tvScore中是否存在标识为R.layout.activity_level的文字视图。如果您有多个布局,请确保所有布局都包含此文本视图。