0

我正在尝试创建自定义 Toast 并在用户回答问题时显示它。这是一个真假游戏,用户得到一个问题并且必须回答,这取决于正确或错误的答案,我使用不同的祝酒词。我有布局,来自 Android 开发者页面。但我不断收到错误(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 采用 Integer 参数。

这是日志猫:

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

0

I have solved it!

I had to create the declare the toast with

Toast t = Toast.makeText(Context, text, duration));

instead of ......

Toast t = Toast(getApplicationContext()), like described on Android...(?)

Thanks Cheers! :)

于 2013-08-29T07:06:49.730 回答
0

它无法识别 id,从 View 本身中找到 id,尝试使用以下代码

View layout = inf.inflate(R.layout.toast_layout, null);

    ImageView img = (ImageView) layout.findViewById(R.id.toastImageV);
    TextView text = (TextView) layout.findViewById(R.id.toastTextV);
于 2013-08-28T14:51:39.253 回答