[英]New dialog activity not starting in Android
I have an activity, that starts the Dialog box when the list item is long pressed. 我有一个活动,长按列表项时会启动对话框。 I start the activity via this code snippet:
我通过以下代码片段开始活动:
private void showWifiSettings(int arg2) {
Intent newIntent = new Intent("com.example.searchingwifi.DIALOGACTIVITY");
startActivity(newIntent);
}
And, the code in the new Dialog activity is as undersigned. 并且,新的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();
}
}
On long pressing the corresponding list item, the Logcat gives me the following error, and the application stops running. 长按相应的列表项时,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
Can anyone help me with the error, and where am I making the mistake ? 谁能帮助我解决错误,我在哪里出错?
EDIT :- Even if simply starting a new Dialog Activity, the system is still giving the error. 编辑:-即使只是开始一个新的对话框活动,系统仍然会给出错误。 The LogCat is as given below.
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
You should check this: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog 您应该检查以下内容: http : //developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
A dialog isn't the same as an Activity. 对话与活动不同。
Currently you must have 目前您必须拥有
public class MainActivity extends Activity{
...
change this to 更改为
public class MainActivity extends FragmentActivity{
...
Then for showing your Dialog add this code in your FragmentActivity 然后为了显示对话框,在FragmentActivity中添加此代码
public void showDialog() {
DialogActivity newDialog = new DialogActivity();
newDialog.show(getSupportFragmentManager(), "myDialog");
} }
Tip: Do not call your custom Dialog class like DialogActivity, but something like MyCustomDialog (not using "Activity" in the class name) 提示:不要像DialogActivity这样调用自定义Dialog类,而要像MyCustomDialog这样调用(不要在类名中使用“ Activity”)
Here you are opening DialogFragment. 在这里,您将打开DialogFragment。 Please try with below code.
请尝试以下代码。
DialogFragment newFragment = new DialogActivity();
newFragment.show(getFragmentManager(), "dialog");
Hope it will help 希望对你有帮助
Updated: Here you are setting LayoutInflator instead of view. 更新:在这里您设置LayoutInflator而不是视图。 That is why it is giving error of input argument.
这就是为什么它给输入参数错误。 Try below:
请尝试以下方法:
@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();
}
}
You are trying to start activity which is actually DialogFragment
. 您正在尝试启动实际上是
DialogFragment
活动。 Either make it as DialogActivity
and start as activity like this: 将其设置为
DialogActivity
并以如下所示的活动开始:
Intent intent=new Intent(getApplicationContext(), DialogActivity.class);
startActivity(intent);
OR 要么
You can keep it as Dialogragment
only, and show as dialogfragment instead of starting a new activity. 您可以将其仅保留为
Dialogragment
,并显示为dialogfragment,而不是开始新的活动。
Example: 例:
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(fragManager, "myDialogFragment");
Reference Links: 参考链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.