我是安卓新手。尝试使用 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