2

这是显示我的对话框活动的代码。每当我尝试打开此意图时,我的应用程序都会强制关闭。所以请告诉我代码中的错误。这里是 logcat。

03-28 12:11:36.851: E/AndroidRuntime(681): 致命异常: main 03-28 12:11:36.851: E/AndroidRuntime(681): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. neelrazin.noteit/com.neelrazin.noteit.Password}: java.lang.NullPointerException 03-28 12:11:36.851: E/AndroidRuntime(681): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03 -28 12:11:36.851: E/AndroidRuntime(681): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-28 12:11:36.851: E/AndroidRuntime(681): 在 android.app .ActivityThread.access$1500(ActivityThread.java:117) 03-28 12:11:36.851: E/AndroidRuntime(681): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 03-28 12: 11:36.851: E/AndroidRuntime(681): 在 android.os.Handler.dispatchMessage(Handler.java:99) 03-28 12:11:36.851: E/AndroidRuntime(681): 在 android.os.Looper.loop(Looper.java:123) 03-28 12:11:36.851: E/AndroidRuntime(681): 在android.app.ActivityThread.main(ActivityThread.java:3683) 03-28 12:11:36.851: E/AndroidRuntime(681): at java.lang.reflect.Method.invokeNative(Native Method) 03-28 12:11 :36.851: E/AndroidRuntime(681): at java.lang.reflect.Method.invoke(Method.java:507) 03-28 12:11:36.851: E/AndroidRuntime(681): at com.android.internal。 os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-28 12:11:36.851: E/AndroidRuntime(681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03 -28 12:11:36.851: E/AndroidRuntime(681): at dalvik.system.NativeStart.main(Native Method) 03-28 12:11:36.851: E/AndroidRuntime(681): Caused by: java.lang. NullPointerException 03-28 12:11:36。851: E/AndroidRuntime(681): 在 com.neelrazin.noteit.Password.onCreate(Password.java:57) 03-28 12:11:36.851: E/AndroidRuntime(681): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047) 03-28 12:11:36.851: E/AndroidRuntime(681): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

public class Password extends Activity {

    final Context context = this;
     public Long mRowId;
     private NotesDbAdapter mDbHelper;
     String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.password);


        mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
                                    : null;
        }

         mDbHelper = new NotesDbAdapter(this);
            mDbHelper.open();
          Cursor notesC = mDbHelper. fetchNote( mRowId);
            startManagingCursor(notesC);
            final String pass = notesC.getString( notesC.getColumnIndex("passw") );


         // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.password);
            dialog.setTitle("Enter Password");

            final EditText enterpass = (EditText) dialog.findViewById(R.id.edtext);

            Button ok=(Button)findViewById(R.id.dialogButtonOK);
            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    password= enterpass.getText().toString();
                    if(password.equals(pass.toString())){

                        Intent i= new Intent(getBaseContext(),NoteEditor.class);
                          i.putExtra(NotesDbAdapter.KEY_ROWID,mRowId);
                            startActivity(i);
                    }

                }
            });

            Button dialogButton = (Button)          dialog.findViewById(R.id.dialogButtonCANCEL);
            dialogButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });
            dialog.show();  
    } 
4

2 回答 2

4

在这里 :

Button ok=(Button)findViewById(R.id.dialogButtonOK);//<<

setContentView在访问视图之前,您忘记调用Activity。将Activity中的Activity布局设置onCreate为:

@Override
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); //<<<
   .....

或者如果dialogButtonOK按钮在 Dialog 内部,则使用 Dialog 实例将ok按钮初始化为:

Button ok=(Button)dialog.findViewById(R.id.dialogButtonOK);
于 2013-03-28T06:49:44.747 回答
2

似乎错误,Buttonok因为初始化以错误的方式完成,

这边走!:

按钮确定=(按钮)dialog.findViewById(R.id.dialogBu​​ttonOK);

反而:

按钮确定=(按钮)findViewById(R.id.dialogBu​​ttonOK);

于 2013-03-28T06:51:31.863 回答