ClassCastException on inflating view for custom dialog

I'm trying to add a custom dialog type to an Android app, but whenever I click on the button that should bring up the dialog, I am gaining strength instead.

The magazineโ€™s exit from closing forces is as follows:

06-05 22:53:28.413: ERROR/AndroidRuntime(187): Uncaught handler: thread main exiting due to uncaught exception
06-05 22:53:28.453: ERROR/AndroidRuntime(187): java.lang.IllegalStateException: Could not execute method of the activity
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.View$1.onClick(View.java:2027)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.View.performClick(View.java:2344)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.View.onTouchEvent(View.java:4133)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.widget.TextView.onTouchEvent(TextView.java:6510)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.View.dispatchTouchEvent(View.java:3672)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.os.Looper.loop(Looper.java:123)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.app.ActivityThread.main(ActivityThread.java:4203)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at java.lang.reflect.Method.invoke(Method.java:521)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at dalvik.system.NativeStart.main(Native Method)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): Caused by: java.lang.reflect.InvocationTargetException
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at nocom.autophage.bikecalc.BikeCalcMainMenu.showAddMeasurementDialog(BikeCalcMainMenu.java:69)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at java.lang.reflect.Method.invoke(Method.java:521)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.view.View$1.onClick(View.java:2022)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     ... 21 more
06-05 22:53:28.453: ERROR/AndroidRuntime(187): Caused by: java.lang.ClassCastException: android.widget.Button
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at nocom.autophage.bikecalc.BikeCalcMainMenu.onCreateDialog(BikeCalcMainMenu.java:48)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.app.Activity.createDialog(Activity.java:867)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     at android.app.Activity.showDialog(Activity.java:2408)
06-05 22:53:28.453: ERROR/AndroidRuntime(187):     ... 25 more

The code that creates the dialog box looks like this: case DIALOG_ADD_MEASUREMENT: AlertDialog.Builder addMeasurementBuilder;

        Context mContext = getApplicationContext();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.add_measurement_dialog,
                                       (ViewGroup) findViewById(R.id.add_measurement_dialog));
        addMeasurementBuilder = new AlertDialog.Builder(mContext);
        addMeasurementBuilder.setView(layout);
        dialog = addMeasurementBuilder.create();
        break;

And XML (which is the part I'm most sure about) in the dialog box:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/add_measurement_dialog">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add_measurement_dialog_title" />

<Spinner
android:id="@+id/add_measurement_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/add_measurement_prompt" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/add_measurement_value" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/calc"
android:onClick="showFeatureNotYetImplementedToast" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/cancel"
    android:onClick="showFeatureNotYetImplementedToast" />

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/add"
    android:onClick="showFeatureNotYetImplementedToast" />  

</LinearLayout>

</LinearLayout>

The only thing I could think of is that maybe I should not use the xml root element id for the dialog as a group of views ... but I canโ€™t understand what else I put in the scam call.

EDIT: BikeCalcMainMenu.java:    nocom.autophage.bikecalc;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class BikeCalcMainMenu extends Activity {
    /** Called when the activity is first created. */

    static final int DIALOG_HELP = 0;
    static final int DIALOG_ADD_MEASUREMENT = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        switch(id) {
        case DIALOG_HELP:
            AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
            helpBuilder.setMessage(getText(R.string.h_help_text))
                   .setCancelable(false)
                   .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dismissDialog(DIALOG_HELP);
                       }
                   });
            AlertDialog helpAlert = helpBuilder.create();
            dialog = helpAlert;
            break;
        case DIALOG_ADD_MEASUREMENT:
            AlertDialog.Builder addMeasurementBuilder;
 //         AlertDialog dialog;

            Context mContext = getApplicationContext();
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.add_measurement_dialog, (ViewGroup) findViewById(R.id.add_measurement_dialog));
            addMeasurementBuilder = new AlertDialog.Builder(mContext);
            addMeasurementBuilder.setView(layout);
            dialog = addMeasurementBuilder.create();
            break;
        default:
            dialog = null;
        }
        return dialog;
    }

    public void showHelpScreenDialog(View v) {
        showDialog(DIALOG_HELP);
    }

    public void showNotYetImplementedToast(View v) {
        Toast not_yet_implemented_toast = Toast.makeText(getApplicationContext(), getText(R.string.feature_not_implemented), Toast.LENGTH_SHORT);
        not_yet_implemented_toast.show();
    }

    public void showAddMeasurementDialog(View v) {
        showDialog(DIALOG_ADD_MEASUREMENT);
    }
}
+3
3

onClick XML?

, ,

(, - , ?)

0

, , .

View layout = inflater.inflate(R.layout.add_measurement_dialog, (ViewGroup) findViewById(R.id.add_measurement_dialog));

, . , null.

http://developer.android.com/reference/android/view/LayoutInflater.html#inflate%28int,%20android.view.ViewGroup%29

0

, .

Try calling addMeasurementBuilder = new AlertDialog.Builder(this);(instead mContext) and see if this works.

In the Dialogs dialog box for Android developers, the result is displayed getApplicationContext(), but I tested the code (copied and pasted) and it will work too! Very bad shape, google.

I found the answer in this question .

0
source

All Articles