我正在尝试创建自定义Toast并在用户回答问题时显示它。这是一个真假游戏,用户得到一个问题,必须回答,根据正确或错误的答案我使用不同的祝酒词。我有布局,来自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采用Integer参数。
这是logcat:
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)
答案 0 :(得分: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);
答案 1 :(得分:0)
我已经解决了!
我必须用
创建声明toastToast t = Toast.makeText(Context, text, duration));
而不是......
Toast t = Toast(getApplicationContext()), like described on Android...(?)
由于 干杯! :)