0

我是安卓新手。尝试使用 DialogFragments 在按下如下按钮时显示对话框

文件:Dialog2/LoginDialog.java

package com.kartnet.Dialog2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.widget.Toast;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.util.Log;

public class LoginDialog extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder ad_builder = new AlertDialog.Builder(getActivity());
        //ad_builder.setMessage(R.string.login_dialog);
        DialogInterface.OnClickListener ad_listener =
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dlg1, int id) {
                final String tag = "CustomDlg";
                Log.d(tag, id + " clicked");
            }
        };

        ad_builder.setTitle(R.string.btn_dialog_custom_title);
        LayoutInflater linf = getActivity().getLayoutInflater();
        ad_builder.setView(linf.inflate(R.layout.dialog_signin, null));

        ad_builder.setNegativeButton("Cancel", ad_listener);
        ad_builder.setPositiveButton("Ok", ad_listener);
        return ad_builder.create();
    }

}

文件:Dialog2/Dialog2App.java

package com.kartnet.Dialog2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.LayoutInflater;
import android.widget.Toast;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.app.DialogFragment;
import android.app.AlertDialog;
import android.util.Log;

public class Dialog2App extends Activity
{
    /* Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
public void displayDialogCustom(View v)
{
    LoginDialog ld = new LoginDialog(); /*<<=== FAILS with runtime exception */
    // ld.show(getFragmentManager(), "login_dialog");
}   /* displayCustomDialog */
}

为什么它会在运行时失败,而在编译时没有错误?另外,由于这两个文件都被声明在同一个包中(com.kartnet.Dialog2),我需要什么特别的东西才能让这两个 java 文件出现在应用程序中吗?

这是显示的错误

I/ActivityManager(   69): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.kartnet.Dialog2/.Dialog2App } from pid 210
I/ActivityManager(   69): Start proc com.kartnet.Dialog2 for activity com.kartnet.Dialog2/.Dialog2App: pid=1328 uid=10034 gids={1015}
W/dalvikvm( 1328): Unable to resolve superclass of Lcom/kartnet/Dialog2/LoginDialog; (7)
W/dalvikvm( 1328): Link of class 'Lcom/kartnet/Dialog2/LoginDialog;' failed
E/dalvikvm( 1328): Could not find class 'com.kartnet.Dialog2.LoginDialog', referenced from method com.kartnet.Dialog2.Dialog2App.displayDialogCustom
W/dalvikvm( 1328): VFY: unable to resolve new-instance 25 (Lcom/kartnet/Dialog2/LoginDialog;) in Lcom/kartnet/Dialog2/Dialog2App;
D/dalvikvm( 1328): VFY: replacing opcode 0x22 at 0x0000
D/dalvikvm( 1328): VFY: dead code 0x0002-0005 in Lcom/kartnet/Dialog2/Dialog2App;.displayDialogCustom (Landroid/view/View;)V
I/ActivityManager(   69): Displayed com.kartnet.Dialog2/.Dialog2App: +1s956ms
D/dalvikvm(  317): GC_EXPLICIT freed 19K, 55% free 2588K/5703K, external 1625K/2137K, paused 3208ms
D/AndroidRuntime( 1328): Shutting down VM
W/dalvikvm( 1328): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 1328): FATAL EXCEPTION: main
E/AndroidRuntime( 1328): java.lang.IllegalStateException: Could not execute method of the activity
E/AndroidRuntime( 1328):    at android.view.View$1.onClick(View.java:2144)
E/AndroidRuntime( 1328):    at android.view.View.performClick(View.java:2485)
E/AndroidRuntime( 1328):    at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime( 1328):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1328):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1328):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1328):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 1328):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1328):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1328):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1328):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1328):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1328): Caused by: java.lang.reflect.InvocationTargetException
E/AndroidRuntime( 1328):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1328):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1328):    at android.view.View$1.onClick(View.java:2139)
E/AndroidRuntime( 1328):    ... 11 more
E/AndroidRuntime( 1328): Caused by: java.lang.NoClassDefFoundError: com.kartnet.Dialog2.LoginDialog
E/AndroidRuntime( 1328):    at com.kartnet.Dialog2.Dialog2App.displayDialogCustom(Dialog2App.java:104)
E/AndroidRuntime( 1328):    ... 14 more
4

5 回答 5

1

根据附加的日志,您似乎正在尝试在不提供对Fragments 的本机支持的旧设备上运行代码:

无法解析 Lcom/kartnet/Dialog2/LoginDialog 的超类;

以上基本上意味着android.app.DialogFragment无法解决,这将发生在 pre-Honeycomb (< Android 3.0 / API level 11) 设备上。您可能正在寻找作为支持库(在包中)Fragment一部分的向后兼容的类。android.support.v4.app.*

要将您的代码迁移到支持库中的类,您应该:

  1. 设置支持库
  2. 更改对(以及您可能正在使用的任何其他类)的所有android.app.DialogFragment引用。android.support.v4.app.DialogFragmentFragment
  3. 将您的更改ActivityFragmentActivity(也位于支持库中)。
于 2013-08-13T07:07:26.283 回答
0

使用此对话框管理器来显示简单的对话框。

public class AlertDialogManager {
/**
 * Function to display simple Alert Dialog
 * @param context - application context
 * @param title - alert dialog title
 * @param message - alert message
 * @param status - success/failure (used to set icon)
 *               - pass null if you don't want icon
 * */
public void showAlertDialog(Context context, String title, String message,
        Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    if(status != null)
        // Setting alert dialog icon
        alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });

    // Showing Alert Message
    alertDialog.show();
}

}

在您的活动中,您可以使用它

alert.showAlertDialog(MainActivity.this,"Header","Message", false);
于 2013-08-13T06:30:17.557 回答
0

尝试这个:

Properties->Java Build Path->Order and Export并检查 Android Private Libraries

于 2013-08-13T06:31:45.197 回答
0

您可以尝试在返回之前创建对话框

Dialog d = ad_builder.create();

return d;

看看它是否无法创建对话框。

于 2013-08-13T06:41:40.193 回答
0

DialogFragment 是单例,所以你不能这样做:

LoginDialog ld = new LoginDialog(); /*<<=== FAILS with runtime exception */

你应该使用 LoginDialog.instantiate(...) 方法

于 2017-04-18T13:21:02.807 回答