新的对话框活动无法在Android中启动

时间:2014-06-27 07:33:14

标签: android android-activity

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

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

任何人都可以帮我解决错误,我在哪里犯错误?

编辑: - 即使只是简单地启动一个新的Dialog Activity,系统仍然会给出错误。 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

3 个答案:

答案 0 :(得分:0)

你应该检查一下: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

对话框与活动不同。

目前你必须

public class MainActivity extends Activity{
...

将此更改为

public class MainActivity extends FragmentActivity{
...

然后显示您的Dialog在FragmentActivity中添加此代码

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

}

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

答案 1 :(得分:0)

这里是打开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();
    }
}

答案 2 :(得分:0)

您正在尝试启动实际为DialogFragment的活动。将其设为DialogActivity并以此活动开始:

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

您只能将其保留为Dialogragment,并显示为对话碎片而非开始新活动。

示例:

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

参考链接:

Dialog Fragment example

Dialog Activity example