当我运行此代码时,我收到异常:无法启动活动 ComponentInfo...AdminSettingsActivity:Java.lang.NullPointerExcepiton。任何人都知道,什么会导致这个问题/异常?我有以下代码:
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class AdminSettingsActivity extends PreferenceActivity {
private Dialog dialog;
Button btn_ok;
Button btn_cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.admin_preferences);
showPasswordDialog();
}
private void showPasswordDialog() {
// create dialog
dialog = new Dialog(this);
dialog.setContentView(R.layout.password_dialog);
dialog.setTitle("Type password...");
btn_ok = (Button) findViewById(R.id.btn_ok_pass_dialog);
btn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Log.d("", "dismis");
}
});
/*
btn_cancel = (Button) findViewById(R.id.btn_cancel_pass_dialog);
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Log.d("", "ok");
}
});
*/
dialog.show();
}
}
并且有 admin_preference.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference android:title="title_web_page" >
<intent android:action="cz.example.k2.PASSWORD_DIALOG" />
</Preference>
</PreferenceScreen>
和password_dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_text_pass_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_cancel_pass_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Zrušit" />
<Button
android:id="@+id/btn_ok_pass_dialog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="OK" />
</LinearLayout>
</LinearLayout>
清单.xml:
...
<activity
android:name="cz.example.k2.AdminSettingsActivity"
android:label="@string/admin_settings_activity_label" >
<intent-filter>
<action android:name="cz.example.k2.ADMIN_SETTINGS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
...