사용자 토스트를 만들고 사용자가 질문에 대답 할 때 표시하려고합니다. 그것은 사용자가 질문을 받고 답변을해야하는 참 거짓 게임입니다. 나는 다른 토스트를 사용하는 옳고 그른 대답에 따라 다릅니다. 안드로이드 개발자 페이지에서 레이아웃이 있습니다. 하지만 오류가 계속 (NullPointerException). 그것은 온 클릭 방법에,맞춤 토스트 만들기 android
<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"
/>
을 그리고 여기에 자바 코드입니다 :
여기에 XML입니다.
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)
오류와 해당 줄이 없으면 많이 도움이되지 않습니다. –
로그를 게시하십시오 – JiTHiN
인플레이터 루트가 NULL이어야합니다. 인플레이터에서 [ViewGroup] findViewById (R.id.toast_layout_root)]를 제거하고 null 값으로 대체하십시오. –