自定义Toast应用程序崩溃点击

时间:2016-05-30 11:59:41

标签: android

所以我有一个自定义吐司,它的工作正常,除了曾经出现在屏幕上并点击它,应用程序崩溃的事实。 的 toast.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="10dp"
android:background="#70716f" >

<ImageView android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp" />

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

爪哇:

    public Toast doToast(String chooseToast) {
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.toast,
            (ViewGroup) findViewById(R.id.toast_layout_root));

    ImageView image = (ImageView) layout.findViewById(R.id.image);
    image.setImageResource(R.drawable.icon_attention);
    TextView text = (TextView) layout.findViewById(R.id.text);
    text.setText(chooseToast);

    Toast toast = new Toast(getApplicationContext());
    toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setView(layout);
    return toast;
}

所以,如果我想创建一个带有“Hey”消息的Toast,那就像是:

Toast messageToast = doToast("Hey");
messageToast.show();

崩溃日志:

05-30 14:48:47.799 19171-19171/aviadapps.calcforme E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: aviadapps.calcforme, PID: 19171
                                                                 java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:289)
                                                                     at android.view.View.performClick(View.java:5217)
                                                                     at android.view.View$PerformClick.run(View.java:21342)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5551)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                  Caused by: java.lang.reflect.InvocationTargetException
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
                                                                     at android.view.View.performClick(View.java:5217) 
                                                                     at android.view.View$PerformClick.run(View.java:21342) 
                                                                     at android.os.Handler.handleCallback(Handler.java:739) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5551) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
                                                                  Caused by: java.lang.NumberFormatException: Invalid double: ""
                                                                     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                     at java.lang.StringToReal.parseDouble(StringToReal.java:267)
                                                                     at java.lang.Double.parseDouble(Double.java:301)
                                                                     at aviadapps.calcforme.MainActivity.calculateClicked(MainActivity.java:36)
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
                                                                     at android.view.View.performClick(View.java:5217) 
                                                                     at android.view.View$PerformClick.run(View.java:21342) 
                                                                     at android.os.Handler.handleCallback(Handler.java:739) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5551) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

我该如何解决它? :)

提前致谢!

1 个答案:

答案 0 :(得分:1)

public void showToast(String message){
 Context context = getApplicationContext();
    // Create layout inflator object to inflate toast.xml file
    LayoutInflater inflater = getLayoutInflater();

    // Call toast.xml file for toast layout 
    View toastRoot = inflater.inflate(R.layout.toast, null);

 ImageView image = (ImageView) toastRoot.findViewById(R.id.image);
image.setImageResource(R.drawable.icon_attention);
TextView text = (TextView) toastRoot.findViewById(R.id.text);
text.setText(chooseToast);

    Toast toast = new Toast(context);

    // Set layout to toast 
    toast.setView(toastRoot);
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,
            0, 0);
    toast.setDuration(Toast.LENGTH_LONG);
    toast.show();

}