0

カスタム Toast を作成し、ユーザーが質問に回答したときに表示しようとしています。これは True-False ゲームで、ユーザーは質問を受けて、答えが正しいか間違っているかに応じて、さまざまなトーストを使用して答える必要があります。Android Developersページからのレイアウトがあります。しかし、エラー(NullPointerException)が発生し続けます。

XML は次のとおりです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/toast_layout_root"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="8dp"
          android:background="#DAAA"
          >
<ImageView android:id="@+id/toastImageV"
           android:src="@drawable/correcticon"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginRight="8dp"

           />
<TextView android:id="@+id/toastTextV"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textColor="#FFF"
          />

これが Java コードです。これは onClick メソッドにあります。

private void checkAnswer(int ans) {

    try{
    if(ans == q.getAnswer()){


        LayoutInflater inf = getLayoutInflater();
        View layout = inf.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

        ImageView img = (ImageView) findViewById(R.id.toastImageV);
        TextView text = (TextView) findViewById(R.id.toastTextV);



        Toast t = new Toast(getApplicationContext());
        img.setImageResource(R.drawable.correcticon);
        text.setText(answerLang[0]);

        t.setDuration(Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.setView(layout);
        t.show();
        getNewTF();
    }
    else{
        LayoutInflater inf = getLayoutInflater();
        View layout = inf.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

        ImageView img = (ImageView) findViewById(R.id.toastImageV);
        TextView text = (TextView) findViewById(R.id.toastTextV);



        Toast t = new Toast(getApplicationContext());
        img.setImageResource(R.drawable.wronganswer);
        text.setText(answerLang[1]);

        t.setDuration(Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER, 0, 0);
        t.setView(layout);
        t.show();
        getNewTF();
    }
}
catch(Exception e){
    e.printStackTrace();
}
}

checkAnswer は整数パラメーターを取ります。

ログキャットは次のとおりです。

08-28 17:05:33.698: W/System.err(13594): java.lang.NullPointerException
08-28 17:05:33.718: W/System.err(13594):    at com.boilingstocks.qdroid.TrueFalse.checkAnswer(TrueFalse.java:363)
08-28 17:05:33.718: W/System.err(13594):    at com.boilingstocks.qdroid.TrueFalse.onClick(TrueFalse.java:331)
08-28 17:05:33.718: W/System.err(13594):    at android.view.View.performClick(View.java:4147)
08-28 17:05:33.718: W/System.err(13594):    at android.view.View$PerformClick.run(View.java:17161)
08-28 17:05:33.718: W/System.err(13594):    at android.os.Handler.handleCallback(Handler.java:615)
08-28 17:05:33.718: W/System.err(13594):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-28 17:05:33.718: W/System.err(13594):    at android.os.Looper.loop(Looper.java:213)
08-28 17:05:33.718: W/System.err(13594):    at android.app.ActivityThread.main(ActivityThread.java:4787)
08-28 17:05:33.718: W/System.err(13594):    at java.lang.reflect.Method.invokeNative(Native Method)
08-28 17:05:33.718: W/System.err(13594):    at java.lang.reflect.Method.invoke(Method.java:511)
08-28 17:05:33.718: W/System.err(13594):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-28 17:05:33.728: W/System.err(13594):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-28 17:05:33.728: W/System.err(13594):    at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2