3

I'm trying to add a custom dialog type to an Android application, but whenever I press the button that should bring up the dialog, I get a force close instead.

The log output from the force close 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's creating the dialog looks like: 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 the XML (which is the part I'm most confident about) for the dialog is:

<?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 was that maybe I shouldn't be using the id of the root element of the xml for the dialog as the viewgroup... but I can't figure out what else I'd put in the inflater call.

EDIT: And the complete contents of BikeCalcMainMenu.java: package 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);
    }
}
autophage
  • 196
  • 1
  • 9
  • is it possible to share code of BikeCalcMainMenu.java? as currently shared code is not what causing an error – TeaCupApp Jun 05 '11 at 23:39
  • Done... thanks for taking a look. I can't see how the rest of the class would help, but I also can't see what's causing the force close to begin with :). – autophage Jun 06 '11 at 01:15

3 Answers3

0

I've just banged my head against a wall or two while solving a similar problem.

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

The Dialogs tutorial on android developers passes in the result of getApplicationContext(), but I've tested the code (copied and pasted) and it crashes too! Very bad form, Google.

I found the answer in this question.

Community
  • 1
  • 1
M_M
  • 1,955
  • 2
  • 17
  • 23
0

Have you tried removing the onClick attributes in the XML?

There could be a problem when it tries to bind them, since they don't exist

(also, any particular reason you're using the application context?)

Christopher Souvey
  • 2,890
  • 21
  • 21
  • I'm using the application context because I'm new to this, and it was copy and pasted. Is there something else I should use? I tried removing the onClick attributes, and got the same error that I posted in response to zienkikk... the same error as I posted above until the end, where the final "Caused by" is a "WindowManagerBadTokenException: Unable to add window -- token null is not for an application." – autophage Jun 05 '11 at 23:48
  • 1
    Every Activity is also a context. So you can just use "this". Unfortunately I'm not sure about the error... – Christopher Souvey Jun 06 '11 at 17:00
0

I haven't tested this but I believe that this line causes your problem

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

If you provide the second argument to inflate then you actually get that parent group back. Consider just passing in null here.

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

zienkikk
  • 2,404
  • 1
  • 21
  • 28
  • Not quite... this gave me a crash that looked the same in the emulator but had a slightly different log file: `06-05 23:21:54.874: ERROR/AndroidRuntime(209): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application – autophage Jun 05 '11 at 23:23
  • Thanks! In my case for a custom view I changed `inflate(context, ...)` to `LayoutInflater.from(context).inflate(...`. – CoolMind Aug 15 '23 at 06:00