Binoy Babu
Binoy Babu

Reputation: 17139

AlertDialog Force Close

Here's the code I'm using to make a custom AlertDialog.

public void onButtonClicked(View v) {
    LayoutInflater onButtonClick = LayoutInflater.from(this);
    PropManager propmanager = new PropManager();
    final View onButtonClickView = onButtonClick.inflate(
            R.layout.buttondialog, null);
    TextView mbname = (TextView) findViewById(R.id.mbname);
    TextView mbdisplayid = (TextView) findViewById(R.id.mbdisplayid);
    TextView mbdevice = (TextView) findViewById(R.id.mbdevice);
    TextView mbkernel = (TextView) findViewById(R.id.mbkernel);
    TextView mbvspath = (TextView) findViewById(R.id.mbvspath);

    String mbvspathv = propmanager.multiBootVSPathProp();
    String mbkernelv = propmanager.kernelProp();
    String mbdevidev = propmanager.deviceProp();
    String mdisplayidv = propmanager.displayIdProp();
    String mbnanev = propmanager.vSNameProp();
    mbname.setText(mbnanev);
    mbdisplayid.setText(mdisplayidv);
    mbdevice.setText(mbdevidev);
    mbkernel.setText(mbkernelv);
    mbvspath.setText(mbvspathv);
    new AlertDialog.Builder(this).setView(onButtonClickView).show();

}

public String vSNameProp() {
    String mbvsprop = "none";
    try {
        Scanner scanner = new Scanner(propReader()).useDelimiter("\\n");
        scanner.findWithinHorizon(
                Pattern.compile("\\[ro.multiboot.vs\\].*\\[(.+?)\\]"), 0);
        mbvsprop = scanner.match().group(1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    File vs = new File(mbvsprop);
    return "Active VFS\t: " + vs.getName();
}

But it force closes on clicking Button.

03-30 17:01:59.717: W/System.err(9802): java.lang.IllegalStateException
03-30 17:01:59.727: W/System.err(9802):     at java.util.Scanner.match(Scanner.java:963)
03-30 17:01:59.727: W/System.err(9802):     at com.manager.boot.free.PropManager.vSNameProp(PropManager.java:138)
03-30 17:01:59.737: W/System.err(9802):     at com.manager.boot.free.OSListActivity.onButtonClicked(OSListActivity.java:1177)
03-30 17:01:59.737: W/System.err(9802):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 17:01:59.737: W/System.err(9802):     at java.lang.reflect.Method.invoke(Method.java:511)
03-30 17:01:59.737: W/System.err(9802):     at android.view.View$1.onClick(View.java:3039)
03-30 17:01:59.737: W/System.err(9802):     at android.view.View.performClick(View.java:3511)
03-30 17:01:59.737: W/System.err(9802):     at android.view.View$PerformClick.run(View.java:14105)
03-30 17:01:59.737: W/System.err(9802):     at android.os.Handler.handleCallback(Handler.java:605)
03-30 17:01:59.737: W/System.err(9802):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 17:01:59.747: W/System.err(9802):     at android.os.Looper.loop(Looper.java:137)
03-30 17:01:59.757: W/System.err(9802):     at android.app.ActivityThread.main(ActivityThread.java:4424)
03-30 17:01:59.757: W/System.err(9802):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 17:01:59.767: W/System.err(9802):     at java.lang.reflect.Method.invoke(Method.java:511)
03-30 17:01:59.767: W/System.err(9802):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-30 17:01:59.767: W/System.err(9802):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-30 17:01:59.767: W/System.err(9802):     at dalvik.system.NativeStart.main(Native Method)
03-30 17:01:59.767: D/AndroidRuntime(9802): Shutting down VM
03-30 17:01:59.767: W/dalvikvm(9802): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-30 17:01:59.787: E/AndroidRuntime(9802): FATAL EXCEPTION: main
03-30 17:01:59.787: E/AndroidRuntime(9802): java.lang.IllegalStateException: Could not execute method of the activity
03-30 17:01:59.787: E/AndroidRuntime(9802):     at android.view.View$1.onClick(View.java:3044)
03-30 17:01:59.787: E/AndroidRuntime(9802):     at android.view.View.performClick(View.java:3511)
03-30 17:01:59.787: E/AndroidRuntime(9802):     at android.view.View$PerformClick.run(View.java:14105

)

Why?

Upvotes: 0

Views: 348

Answers (2)

Win Myo Htet
Win Myo Htet

Reputation: 5457

It has nothing to do with AlertDialog. Your code dealing with the Scanner is giving you problem. You need to separately debug the Scanner code.

Upvotes: 1

FoamyGuy
FoamyGuy

Reputation: 46856

I can't exactly tell what is going wrong with your code.(It is harder because your variable names are not very descriptive. i.e. there is no obvious reason why reference that is named "onButtonClick" would be a reference to a LayoutInflater object. It makes it more confusing to understand what is going on.) But here is a snippet that you can start from that will create a custom AlertDialog:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.yourLayoutId, (ViewGroup) findViewById(R.id.yourLayoutRoot));
    AlertDialog.Builder builder = new AlertDialog.Builder(this)
    .setView(layout);
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

Also, I think (but am not positive) that you would need to change your findViewById() calls to be called on you view instance, not your activity as you are now. so change:

TextView mbname = (TextView) findViewById(R.id.mbname);
etc...

to

TextView mbname = (TextView) onButtonClickView.findViewById(R.id.mbname);

Upvotes: 2

Related Questions