Java.lang.nullpointer异常(字体)

时间:2012-04-24 20:07:47

标签: java android font-face

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

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

这很奇怪,因为每个人都有字体,等等:(

即使是共同的敬意也应该有效!!

2 个答案:

答案 0 :(得分:0)

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

txt1.setTypeface(Typeface.createFromAsset(getAssets(),"game_over.ttf"));
那么也许这个问题可能有助于解决它: set font at runtime, Textview。您可能需要将字体文件放入assets / fonts目录(如果它们尚未存在)。

答案 1 :(得分:0)

我发现了问题是什么

未正确加载SharedPreferences,因此无法加载