繁体   English   中英

新对话框活动未在Android中启动

[英]New dialog activity not starting in Android

我有一个活动,长按列表项时会启动对话框。 我通过以下代码片段开始活动:

private void showWifiSettings(int arg2) {
                Intent newIntent = new Intent("com.example.searchingwifi.DIALOGACTIVITY");
                startActivity(newIntent);
                }

并且,新的Dialog活动中的代码已签名。

package com.example.searchingwifi;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;

public class DialogActivity extends DialogFragment{


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.dialog, null));


        return builder.create();
    }
}

长按相应的列表项时,Logcat给我以下错误,并且应用程序停止运行。

06-27 12:54:22.150: E/AndroidRuntime(25118): FATAL EXCEPTION: main
06-27 12:54:22.150: E/AndroidRuntime(25118): Process: com.example.searchingwifi, PID: 25118
06-27 12:54:22.150: E/AndroidRuntime(25118): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.searchingwifi/com.example.searchingwifi.DialogActivity}: java.lang.ClassCastException: com.example.searchingwifi.DialogActivity cannot be cast to android.app.Activity
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.os.Handler.dispatchMessage(Handler.java:102)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.os.Looper.loop(Looper.java:136)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.ActivityThread.main(ActivityThread.java:5017)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at java.lang.reflect.Method.invoke(Method.java:515)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at dalvik.system.NativeStart.main(Native Method)
06-27 12:54:22.150: E/AndroidRuntime(25118): Caused by: java.lang.ClassCastException: com.example.searchingwifi.DialogActivity cannot be cast to android.app.Activity
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-27 12:54:22.150: E/AndroidRuntime(25118):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
06-27 12:54:22.150: E/AndroidRuntime(25118):    ... 11 more

谁能帮助我解决错误,我在哪里出错?

编辑:-即使只是开始一个新的对话框活动,系统仍然会给出错误。 LogCat如下所示。

06-30 03:54:10.550: E/AndroidRuntime(1175): FATAL EXCEPTION: main
06-30 03:54:10.550: E/AndroidRuntime(1175): Process: com.example.testingdialog, PID: 1175
06-30 03:54:10.550: E/AndroidRuntime(1175): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testingdialog/com.example.testingdialog.MainActivity}: java.lang.ClassCastException: com.example.testingdialog.MainActivity cannot be cast to android.app.Activity
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.os.Looper.loop(Looper.java:136)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at java.lang.reflect.Method.invokeNative(Native Method)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at java.lang.reflect.Method.invoke(Method.java:515)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at dalvik.system.NativeStart.main(Native Method)
06-30 03:54:10.550: E/AndroidRuntime(1175): Caused by: java.lang.ClassCastException: com.example.testingdialog.MainActivity cannot be cast to android.app.Activity
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-30 03:54:10.550: E/AndroidRuntime(1175):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
06-30 03:54:10.550: E/AndroidRuntime(1175):     ... 11 more

您应该检查以下内容: http : //developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

对话与活动不同。

目前您必须拥有

public class MainActivity extends Activity{
...

更改为

public class MainActivity extends FragmentActivity{
...

然后为了显示对话框,在FragmentActivity中添加此代码

public void showDialog() {
DialogActivity newDialog = new DialogActivity();
newDialog.show(getSupportFragmentManager(), "myDialog");

}

提示:不要像DialogActivity这样调用自定义Dialog类,而要像MyCustomDialog这样调用(不要在类名中使用“ Activity”)

在这里,您将打开DialogFragment。 请尝试以下代码。

DialogFragment newFragment = new DialogActivity();
newFragment.show(getFragmentManager(), "dialog");

希望对你有帮助

更新:在这里您设置LayoutInflator而不是视图。 这就是为什么它给输入参数错误。 请尝试以下方法:

@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog, null);
        builder.setView(view);

        return builder.create();
    }
}

您正在尝试启动实际上是DialogFragment活动。 将其设置为DialogActivity并以如下所示的活动开始:

Intent intent=new Intent(getApplicationContext(), DialogActivity.class);
startActivity(intent);

要么

您可以将其仅保留为Dialogragment ,并显示为dialogfragment,而不是开始新的活动。

例:

MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(fragManager, "myDialogFragment");

参考链接:

对话框片段示例

对话框活动示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM