Answer the question
In order to leave comments, you need to log in
Why does the app crash when switching to another activity?
'Notes' application
When USB debugging starts, main_activity opens, and when you press the '+' button, the application closes.
I've already broken my head, what's wrong ....
Help me find the error, please.
MainActivity:
package com.example.testing_interface.screens;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.testing_interface.R;
import com.example.testing_interface.model.Note;
import com.example.testing_interface.screens.detals.NoteDetailsActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
final Adapter adapter = new Adapter();
recyclerView.setAdapter(adapter);
Button add_button = (Button) findViewById(R.id.button_add);
add_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NoteDetailsActivity.start(MainActivity.this, null);
}
});
MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainViewModel.getNoteLiveData().observe(this, new Observer<List<Note>>() {
@Override
public void onChanged(List<Note> notes) {
adapter.setItems(notes);
}
});
}
}
<?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"
android:background="@color/mainBgColor"
tools:context=".screens.MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="50dp"
android:layout_height="0dp"
android:background="@color/sideBgColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/button_add"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="400dp"
android:background="@android:drawable/ic_input_add"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/notes_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:text="@string/app_headle"
android:textColor="@android:color/background_dark"
android:textSize="40sp"
app:layout_constraintStart_toEndOf="@+id/constraintLayout"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="359dp"
android:layout_height="563dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/constraintLayout"
app:layout_constraintTop_toBottomOf="@+id/notes_header" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.testing_interface.screens.detals;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toolbar;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.testing_interface.App;
import com.example.testing_interface.R;
import com.example.testing_interface.model.Note;
public class NoteDetailsActivity extends AppCompatActivity {
private static final String EXTRA_NOTE = "NoteDetailsActivity.EXTRA_NOTE";
private Note note;
private EditText editText;
public static void start(Activity caller, Note note){
Intent intent = new Intent(caller, NoteDetailsActivity.class);
if(note != null){
intent.putExtra(EXTRA_NOTE, note);
}
caller.startActivity(intent);
}
private void setSupportActionBar(Toolbar toolbar) {
}
Toolbar toolbar = findViewById(R.id.toolbar);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_details);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setTitle(getString(R.string.note_details_title));
editText = findViewById(R.id.text);
if(getIntent().hasExtra(EXTRA_NOTE)){
note = getIntent().getParcelableExtra(EXTRA_NOTE);
editText.setText(note.text);
} else {
note = new Note();
}
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId())
{
case android.R.id.home:
finish();
break;
case R.id.action_save:
if(editText.getText().length() > 0){
note.text = editText.getText().toString();
note.done = false;
note.timestump = System.currentTimeMillis();
if(getIntent().hasExtra(EXTRA_NOTE)){
App.getInstance().getNoteDao().update(note);
} else {
App.getInstance().getNoteDao().insertAll(note);
}
finish();
}
break;
}
return super.onOptionsItemSelected(item);
}
}
<?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"
android:background="@color/mainBgColor">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/sideBgColor"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:hint="@string/note_text"
android:minHeight="200dp"
android:gravity="top"
android:padding="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar" />
<Button
android:id="@+id/action_save"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="3dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="8dp"
android:background="@android:drawable/ic_menu_save"
app:layout_constraintBottom_toBottomOf="@+id/toolbar"
app:layout_constraintEnd_toEndOf="@+id/toolbar"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Answer the question
In order to leave comments, you need to log in
You NoteDetailsActivity
seem to crash in this place:
setSupportActionBar(toolbar);
Reason 1:
Toolbar toolbar = findViewById(R.id.toolbar);
This field must be initialized in the method onCreate()
and must be initialized after the call setContentView()
, otherwise it findViewById()
returns null
.
Reason 2:
private void setSupportActionBar(Toolbar toolbar) {
}
private void setSupportActionBar(Toolbar toolbar) {
super.setSupportActionBar(toolbar);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question