我想获得" EditText et"的价值。通过调用getText()。toString()方法... 但是由于NullPointerException,我的应用程序崩溃了。
可能是因为我在不创建新活动的情况下将布局" activity_add_question_popup.xml作为PopupWindow。 不幸的是,我是Android Studio的新手。有人能告诉我什么是错的吗?
logcat的:
java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:4007) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.view.View$1.onClick(View.java:4002) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.brandsplorer.neverever20.Settings.saveNewQuestion(Settings.java:203) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.view.View$1.onClick(View.java:4002) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
布局:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/game_title"
android:id="@+id/addQuestion_title"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="20sp"
android:layout_gravity="left|center_horizontal"
android:layout_marginLeft="30sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et"
android:layout_below="@+id/game_title_activity"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|top"
android:layout_alignTop="@+id/addQuestion_title"
android:gravity="left|center_vertical"
android:layout_marginTop="40sp"
android:hint="Gib deine Frage ein..."
android:ems="10" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50sp">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="Speichern"
android:id="@+id/addQuestionSaveBtn"
android:layout_alignTop="@+id/newQuestionEdit"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom|right"
android:onClick="saveNewQuestion" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="110sp"
android:layout_height="wrap_content"
android:text="Zurück"
android:id="@+id/addQuestionBackBtn"
android:layout_alignTop="@+id/newQuestionEdit"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_gravity="left|bottom"
android:onClick="closePopup" />
</FrameLayout>
</LinearLayout>
</FrameLayout>
class:Settings.java
package com.brandsplorer.neverever20;
public class Settings extends ActionBarActivity {
Button settingsBack, addQuestion;
CheckBox chkBox_sex, chkBox_day, chkBox_job, chkBox_alcohol, chkBox_own;
TextView settingsTitle, category;
EditText editText;
LayoutInflater layoutInflater;
View popupView;
PopupWindow popupWindow;
//KEYS
public static final String MyPREFERENCES = "MyPrefs";
public static final String sex = "sexBox";
public static final String day = "dayBox";
public static final String alc = "alcBox";
public static final String job = "jobBox";
public static final String own = "ownBox";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
settingsBack = (Button)findViewById(R.id.settingsBackBtn);
addQuestion = (Button)findViewById(R.id.addQuestionBtn);
chkBox_alcohol = (CheckBox)findViewById(R.id.alcBox);
chkBox_day = (CheckBox)findViewById(R.id.dayBox);
chkBox_sex = (CheckBox)findViewById(R.id.sexBox);
chkBox_job = (CheckBox)findViewById(R.id.jobBox);
chkBox_own = (CheckBox)findViewById(R.id.ownBox);
settingsTitle = (TextView)findViewById(R.id.settings_title);
category = (TextView)findViewById(R.id.kategorien);
//initializing editText
editText = (EditText)findViewById(R.id.et);
loadSavedPreferences();
}
public void backToMain(View view){
if(chkBox_job.isChecked()==false && chkBox_sex.isChecked()==false && chkBox_day.isChecked()==false && chkBox_alcohol.isChecked()==false && chkBox_own.isChecked()==false){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Mindestens eine Kategorie muss ausgewählt sein");
builder.setNegativeButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
else {
savePreferences();
loadSavedPreferences();
finish();
}
}
/*
Here I am getting the Popup.
A button in my activity_settings.xml runs this method over an onClick attribute.
*/
public void addQuestion(View view){
layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.activity_add_question_popup, null);
popupWindow = new PopupWindow(popupView, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(addQuestion, Gravity.CENTER, 0, 0);
}
public void closePopup(View view){
popupWindow.dismiss();
}
@Override
public void onBackPressed() {
if(chkBox_job.isChecked()==false && chkBox_sex.isChecked()==false && chkBox_day.isChecked()==false && chkBox_alcohol.isChecked()==false && chkBox_own.isChecked()==false){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Mindestens eine Kategorie muss ausgewählt sein");
builder.setNegativeButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();
}
else {
savePreferences();
loadSavedPreferences();
finish();
}
}
public void loadSavedPreferences(){
SharedPreferences sharedPreferences = getSharedPreferences("CheckboxData", Context.MODE_PRIVATE);
boolean checkedSex = sharedPreferences.getBoolean(sex, true);
if(checkedSex){
chkBox_sex.setChecked(true);
}
else{
chkBox_sex.setChecked(false);
}
boolean checkedAlc = sharedPreferences.getBoolean(alc, true);
if(checkedAlc){
chkBox_alcohol.setChecked(true);
}
else{
chkBox_alcohol.setChecked(false);
}
boolean checkedDay = sharedPreferences.getBoolean(day, true);
if(checkedDay){
chkBox_day.setChecked(true);
}
else{
chkBox_day.setChecked(false);
}
boolean checkedJob = sharedPreferences.getBoolean(job, true);
if(checkedJob){
chkBox_job.setChecked(true);
}
else{
chkBox_job.setChecked(false);
}
boolean checkedOwn = sharedPreferences.getBoolean(own, false);
if(checkedOwn){
chkBox_own.setChecked(true);
}
else{
chkBox_own.setChecked(false);
}
}
public void savePreferences(){
SharedPreferences sharedPreferences = getSharedPreferences("CheckboxData", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putBoolean(sex, chkBox_sex.isChecked());
editor.putBoolean(alc, chkBox_alcohol.isChecked());
editor.putBoolean(job, chkBox_job.isChecked());
editor.putBoolean(day, chkBox_day.isChecked());
editor.putBoolean(own, chkBox_own.isChecked());
editor.commit();
}
public void saveNewQuestion(View view){
//because of this editText.getText() I get an error.
//editText is null for some reason...
String question = editText.getText().toString();
String file_name = "sentences_own";
if(question.isEmpty()){
Toast.makeText(getApplicationContext(), "Frage darf nicht leer sein!", Toast.LENGTH_LONG).show();
}
else {
try {
FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_APPEND);
fileOutputStream.write(question.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Frage gespeichert!", Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
您获得NullPointerException
,因为EditText
的定义不在您调用它的function
内。在editText = (EditText)findViewById(R.id.et);
功能中移动saveNewQuestion
。