以下是显示我的对话框活动的代码。每当我尝试打开这个意图时,我的应用程序就会强制关闭。所以请告诉我代码中的错误。这里是logcat。
03-28 12:11:36.851:E / AndroidRuntime(681):致命异常:主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):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-28 12:11:36.851:E / AndroidRuntime(681):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)03-28 12:11:36.851:E / AndroidRuntime(681):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 03-28 12:11:36.851:E / AndroidRuntime(681):at android.os.Handler.dispatchMessage(Handler.java:99)03-28 12:11:36.851:E / AndroidRuntime(681):at android.os.Looper.loop(Looper.java:123)03-28 12:11:36.851: E / AndroidRuntime(681):at 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):引起:java.lang.NullPointerException 03-28 12:11:36.851:E / AndroidRuntime(681):at com.neelrazin.noteit.Password.onCreate(Password.java:57)03-28 12:11:36.851:E / AndroidRuntime(681):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-28 12:11:36.851:E / AndroidRuntime(681):at 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();
}
答案 0 :(得分:4)
在这里:
Button ok=(Button)findViewById(R.id.dialogButtonOK);//<<
在从中访问视图之前,您忘记调用setContentView
for Activity。将Activity onCreate
中的Activity设置为:
@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);
答案 1 :(得分:2)
Buttonok
上似乎错误,因为初始化是以错误的方式完成的,
这样!:
按钮ok =(按钮)dialog.findViewById(R.id.dialogButtonOK);
相反:
按钮ok =(按钮)findViewById(R.id.dialogButtonOK);