-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

すべてのラウトがそこにあり、フォントがそこにあるなどがあるので、それは非常に奇妙です:(

共有設定も機能するはずです!!

4

2 に答える 2

0

私は問題が何であるかを知りました

SharedPreferencesが正しくロードされなかったため、ロードに失敗しました

于 2012-04-25T14:42:40.157 に答える
0

例外スタック トレースは display_score.java:48 を指していますが、コードに package ステートメントと import ステートメントが含まれていないと、それがどの行かわかりません。この線なら

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

おそらく、このSOの質問はそれを解決するのに役立つかもしれません: 実行時にフォントを設定、 Textview。フォント ファイルがまだそこにない場合は、assets/fonts ディレクトリに配置する必要がある場合があります。

于 2012-04-24T20:40:01.097 に答える