Mobile Developer Mobile Developer - 26 days ago 5
Android Question

App crash when onClick on 2nd item and so on in listview

I am just a beginner in programming. I am having issue with my code. Whenever I try to click on the 2nd item in a list, the app suddenly crashes.

This is my code:

public class MainActivity extends AppCompatActivity {

ListView lvCategories;
String[] categories = {
"School", "Work", "Family Outings", "Friends Outings", "Groceries" , "Appointments", "Indoor Activities",
"Outdoor Activities", "Games","Overseas Travelling"
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvCategories = (ListView)findViewById(R.id.listViewCategories);

final ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, categories);
lvCategories.setAdapter(adapter);

lvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String category = (String) parent.getItemAtPosition(position);

if (category == "School") {
Intent school = new Intent(view.getContext(), school_activity.class);
school.putExtra("school_category", "");
startActivity(school);
}
if (category == "Work") {
Intent work = new Intent(view.getContext(), work_activity.class);
work.putExtra("work_category", "");
startActivity(work);
}
}
});
}
}


May I know what is the issue? I need to solve this asap.

Here is the error I found from LogCat.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference


And here's my
work_activity


public class work_activity extends AppCompatActivity {

Button btnSave;
EditText etContent;
TextView tvLastUpdated;

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

btnSave = (Button)findViewById(R.id.buttonSave);
etContent = (EditText)findViewById(R.id.editTextContent);
tvLastUpdated = (TextView)findViewById(R.id.textViewLastUpdated);

Intent intentReceived = getIntent();
final String strContent = intentReceived.getStringExtra("work_category");
etContent.setText(strContent);
Calendar now = Calendar.getInstance();
final String datetime = now.get(Calendar.DAY_OF_MONTH) + "/" +
(now.get(Calendar.MONTH) + 1) + "/" +
now.get(Calendar.YEAR) + " " +
now.get(Calendar.HOUR_OF_DAY) + ":" +
now.get(Calendar.MINUTE);
tvLastUpdated.setText(datetime);

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strContentSaved = etContent.getText().toString();
SharedPreferences preferences = getSharedPreferences("category_work_saved", MODE_PRIVATE);
SharedPreferences.Editor prefEdit = preferences.edit();
prefEdit.putString("contentSaved", strContentSaved);
prefEdit.putString("date", datetime);
prefEdit.commit();
Toast.makeText(work_activity.this, "Content Saved Successfully", Toast.LENGTH_SHORT).show();
finish();
}
});
}

@Override
protected void onResume() {
super.onResume();
SharedPreferences preferences = getSharedPreferences("category_work_saved", MODE_PRIVATE);
String contentSaved = preferences.getString("contentSaved", "");
etContent.setText(contentSaved);
String dateTime = preferences.getString("date","");
tvLastUpdated.setText(dateTime);
}
}

Answer

You've some critical problems in your code. Let me point out these issues here.

In your MainActivity you're comparing the String values wrongly. You need to use equals() method instead of == operator comparison to compare String.

And yes, try sending something to the work_activity or school_activity.

lvCategories.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String category = (String) parent.getItemAtPosition(position);

        if (category.equals("School")) {
            Intent school = new Intent(view.getContext(), school_activity.class);
            school.putExtra("school_category", "send something here");
            startActivity(school);
        }
        if (category.equals("Work")) {
            Intent work = new Intent(view.getContext(), work_activity.class);
            work.putExtra("work_category", "send something here");
            startActivity(work);
        }
    }
});

Now in your work_activity, you need to check for null value for received intent.

String strContent = null;
Intent intentReceived = getIntent();

// Check if the intent received is null
if (intentReceived != null) 
    strContent = intentReceived.getStringExtra("work_category");

// Now check if the string content is null 
if(strContent != null) 
    etContent.setText(strContent);

Another thing to check if the id of the EditText in your activity_work_activity layout is editTextContent. The id needs to match to find the EditText which might cause the crash here.