TextView运行不正常

时间:2014-04-01 14:21:51

标签: java android xml textview

我在activity_main.XML中显示了一个显示变量的TextView。但每次我进入应用程序时都会崩溃。

这是xml文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.crosser.MainActivity"
    tools:ignore="MergeRootFrame" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</FrameLayout>

这是MainActivity:

    TextView myText = (TextView) findViewById(R.id.textView1);
    myText.setText("Score:" + result.score);

这里是Logcat的日志

04-01 10:14:01.990: E/AndroidRuntime(925): FATAL EXCEPTION: main
04-01 10:14:01.990: E/AndroidRuntime(925): Process: com.example.crosser, PID: 925
04-01 10:14:01.990: E/AndroidRuntime(925): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crosser/com.example.crosser.MainActivity}: java.lang.NullPointerException
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.os.Handler.dispatchMessage(Handler.java:102)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.os.Looper.loop(Looper.java:136)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.ActivityThread.main(ActivityThread.java:5017)
04-01 10:14:01.990: E/AndroidRuntime(925):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 10:14:01.990: E/AndroidRuntime(925):  at java.lang.reflect.Method.invoke(Method.java:515)
04-01 10:14:01.990: E/AndroidRuntime(925):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-01 10:14:01.990: E/AndroidRuntime(925):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-01 10:14:01.990: E/AndroidRuntime(925):  at dalvik.system.NativeStart.main(Native Method)
04-01 10:14:01.990: E/AndroidRuntime(925): Caused by: java.lang.NullPointerException
04-01 10:14:01.990: E/AndroidRuntime(925):  at com.example.crosser.MainActivity.onCreate(MainActivity.java:55)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.Activity.performCreate(Activity.java:5231)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-01 10:14:01.990: E/AndroidRuntime(925):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-01 10:14:01.990: E/AndroidRuntime(925):  ... 11 more

主要活动中的第55行是:

myText.setText("Score:" + result.score);

结果是一个存储得分为公共变量的类。

我得到了这一行,从开始时得分为0,而不是空。

int score = 0;
Results result = new Results(score);

这是我主要活动中的主要代码:

Car car = new Car( screenWidth/2 , screenHeigh/2 , 15 , screenHeigh, screenWidth);
Person person = new Person( 0 , screenHeigh/2 , 10 , 20 , screenWidth,screenHeigh);
CarView carview = new CarView(this, car);
PersonView personview = new PersonView(this, person);
GameView gameview = new GameView(this);
gameview.setViews(carview, personview);
setContentView(gameview);
CarMover moveCar = new CarMover(car,gameview);
PersonMover movePerson = new PersonMover(person, gameview, result, car);

1 个答案:

答案 0 :(得分:0)

您的result变量可能为空,因此调用result.score会导致NullPointerException

您还需要确保在使用setContentView之前致电findViewById,否则myText可能会为空。