Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a custom Toast android
    primarykey
    data
    text
    <p>I'm trying to create a custom Toast and display it when user answer a question. It's a True-False game where the user gets a question and have to answer, depending on right or wrong answer I use different toasts. I have the layout, From Android Developers page. But I keep getting error (NullPointerException).</p> <p>here's the XML: </p> <pre><code>&lt;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" &gt; &lt;ImageView android:id="@+id/toastImageV" android:src="@drawable/correcticon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" /&gt; &lt;TextView android:id="@+id/toastTextV" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFF" /&gt; </code></pre> <p></p> <p>And here's the Java code, It is in the onClick method.</p> <pre><code>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(); } } </code></pre> <p>The checkAnswer takes an Integer parameter.</p> <p>Here's the logcat:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload