-1

所以可以说我有这样的活动:

public class display_score extends Activity implements OnClickListener {

    Button back;
    TextView txt1;
    TextView txt2;
    TextView txt3;
    TextView txt4;
    TextView txt5;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.display_highscore);


       back = (Button) findViewById(R.id.button1);
        back.setOnClickListener(this);



        txt1 = (TextView) findViewById(R.id.textView1);
        txt2 = (TextView) findViewById(R.id.textView2);
        txt3 = (TextView) findViewById(R.id.textView3);
        txt4 = (TextView) findViewById(R.id.textView4);
        txt5 = (TextView) findViewById(R.id.textView5);



        txt1.setText("1. "+score.scores.getString("NAME","")+" "+String.valueOf(score.scores.getInt("SCORE",0)));
        txt2.setText("2. "+score.scores.getString("NAME2","")+" "+String.valueOf(score.scores.getInt("SCORE2",0)));
        txt3.setText("3. "+score.scores.getString("NAME3","")+" "+String.valueOf(score.scores.getInt("SCORE3",0)));
        txt4.setText("4. "+score.scores.getString("NAME4","")+" "+String.valueOf(score.scores.getInt("SCORE4",0)));
        txt5.setText("5. "+score.scores.getString("NAME5","")+" "+String.valueOf(score.scores.getInt("SCORE5",0)));


        txt1.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt2.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt3.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt4.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
        txt5.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));

        txt1.setTextColor(Color.BLACK);
        txt2.setTextColor(Color.BLACK);
        txt3.setTextColor(Color.BLACK);
        txt4.setTextColor(Color.BLACK);
        txt5.setTextColor(Color.BLACK);


    }

    @Override
    public void onClick(View v) {
        finish();

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }




}

和这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical" 
    android:background="@drawable/background">


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


    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="2"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="3"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="4"
        android:textAppearance="?android:attr/textAppearanceLarge" />



    <TextView
        android:id="@+id/textView5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="5"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Back" />

</LinearLayout>

然后我得到这个异常:

04-24 22:02:43.101: E/AndroidRuntime(10956): FATAL EXCEPTION: main
04-24 22:02:43.101: E/AndroidRuntime(10956): java.lang.RuntimeException: Unable to start activity ComponentInfo{run.alexander.fuchs/run.alexander.fuchs.display_score}: java.lang.NullPointerException
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.os.Looper.loop(Looper.java:137)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at java.lang.reflect.Method.invokeNative(Native Method)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at java.lang.reflect.Method.invoke(Method.java:511)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at dalvik.system.NativeStart.main(Native Method)
04-24 22:02:43.101: E/AndroidRuntime(10956): Caused by: java.lang.NullPointerException
04-24 22:02:43.101: E/AndroidRuntime(10956):    at run.alexander.fuchs.display_score.onCreate(display_score.java:48)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.Activity.performCreate(Activity.java:4465)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-24 22:02:43.101: E/AndroidRuntime(10956):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-24 22:02:43.101: E/AndroidRuntime(10956):    ... 11 more

这很奇怪,因为每个 laout 都在那里,字体在那里等等 :(

即使是共同的偏好也应该起作用!

4

2 回答 2

0

我发现了问题所在

SharedPreferences 未正确加载,因此加载失败

于 2012-04-25T14:42:40.157 回答
0

您的异常堆栈跟踪指向 display_score.java:48,但如果没有包含在您的代码中的包和导入语句,我无法分辨那是哪一行。如果是这条线

txt1.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));

那么也许这个 SO 问题可能有助于解决它: 在运行时设置字体 Textview。如果字体文件不存在,您可能需要将它们放入 assets/fonts 目录中。

于 2012-04-24T20:40:01.097 回答