AlertDialog强制关闭

时间:2012-03-31 00:35:56

标签: android

这是我用来制作自定义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();
}

但点击按钮会强行关闭。

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

为什么?

2 个答案:

答案 0 :(得分:2)

我无法准确地告诉您的代码出了什么问题。(因为你的变量名称不是很具描述性,所以更难了。也就是说没有明显的理由为什么名为“onButtonClick”的引用会引用一个LayoutInflater对象。它让你更容易理解正在发生的事情。)但是这里有一个片段,你可以从它开始创建一个自定义的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();

另外,我认为(但不是肯定的)您需要更改您的查看实例上的findViewById()调用,而不是您现在的活动。所以改变:

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

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

答案 1 :(得分:1)

它与AlertDialog无关。您处理扫描仪的代码给您带来了问题。您需要单独调试扫描程序代码。