Я решил в Android Studio сделать приложение-календарь и добавить туда заметки, но при нажатии кнопки "ОК" в диалоге сохранения заметки, приложение вылетает со следующей ошибкой: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
Код activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/notes"
android:layout_width="92dp"
android:layout_height="57dp"
android:layout_marginStart="149dp"
android:layout_marginLeft="149dp"
android:layout_marginTop="376dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:layout_marginBottom="162dp"
android:text="@string/Notes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RelativeLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="106dp"
app:adSize="BANNER"
app:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
<CalendarView
android:id="@+id/calendarView2"
android:layout_width="296dp"
android:layout_height="298dp"
android:layout_marginStart="54dp"
android:layout_marginLeft="54dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="61dp"
android:layout_marginRight="61dp"
android:layout_marginBottom="275dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Код MainActivity.java:
public class MainActivity extends AppCompatActivity implements OnClickListener {
Button notes;
public AdView mAdView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notes = (Button) findViewById(R.id.notes);
notes.setOnClickListener(this);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.notes:
Intent intent = new Intent(this, Notes.class);
startActivity(intent);
break;
default:
break;
}
}
}
Код activity_notes.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="Notes">
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="83dp"
android:layout_height="73dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginBottom="5dp"
android:contentDescription="@string/addnote"
android:onClick="addnote"
android:src="@android:drawable/ic_input_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="ImageContrastCheck" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="279dp"
android:layout_marginBottom="324dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/final_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/notesView"
android:layout_width="410dp"
android:layout_height="468dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/readnotes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginLeft="158dp"
android:layout_marginTop="476dp"
android:layout_marginEnd="124dp"
android:layout_marginRight="124dp"
android:layout_marginBottom="79dp"
android:onClick="onClickGet"
android:text="@string/Open_notes"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Код Notes.java:
public class Notes extends AppCompatActivity {
final Context context = this;
public ImageButton button;
public TextView final_text;
public SharedPreferences pref;
public EditText input_text;
public TextView notesView;
public final String save_key = "save note";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes);
notesView = (TextView)findViewById(R.id.notesView);
button = findViewById(R.id.imageButton3);
final_text = findViewById(R.id.final_text);
init();}
public void init(){
pref = getSharedPreferences( "note",MODE_PRIVATE);
input_text = findViewById (R.id.input_text);
notesView.setText(pref.getString(save_key,"You haven`t got any note"));
}
public void addnote(View arg0) {
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompt, null);
//Создаем AlertDialog
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(context);
//Настраиваем prompt.xml для нашего AlertDialog:
mDialogBuilder.setView(promptsView);
//Настраиваем отображение поля для ввода текста в открытом диалоге:
final EditText userInput = promptsView.findViewById(R.id.input_text);
//Настраиваем сообщение в диалоговом окне:
mDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
final_text.setText(userInput.getText());
SharedPreferences.Editor edit = pref.edit();
edit.putString(save_key,input_text.getText().toString());
edit.apply();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
//Создаем AlertDialog:
AlertDialog alertDialog = mDialogBuilder.create();
//и отображаем его:
alertDialog.show();
}
public void onClickGet (View view){
notesView.setText(pref.getString(save_key, defValue: "You haven`t got any note"));
}
}
Код promt.xml:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write_the_note"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<requestFocus />
</EditText>
</LinearLayout>
Помогите мне пожалуйста с этой ошибкой.
EditTextвы обращаетесь в обработчике кнопки "ОК" и какое значение присвоено этой ссылке. – woesss Oct 08 '21 at 07:03