gpt4 book ai didi

android - 当我尝试显示对话框时,它给我强制关闭错误

转载 作者:太空狗 更新时间:2023-10-29 16:20:44 24 4
gpt4 key购买 nike

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

03-28 12:11:36.851: E/AndroidRuntime(681): FATAL EXCEPTION: main 03-28 12:11:36.851: E/AndroidRuntime(681): java.lang.RuntimeException: Unable to start activity 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): Caused by: 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();
}

最佳答案

在这里:

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

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

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

或者如果 dialogBu​​ttonOK 按钮在 Dialog 中,则使用 Dialog 实例将 ok 按钮初始化为:

Button ok=(Button)dialog.findViewById(R.id.dialogButtonOK);

关于android - 当我尝试显示对话框时,它给我强制关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com