I am still new to android so sometimes I can’t understand for what reasons some errors occur in it, I didn’t find anything in Google.
In general, there is an activation from the listview and an adapter to it, where the adapter has a model, when you first activate and switch from another activation, everything works fine, then I switch to the third activation and when I try to return to activation with the adapter, NullPointer pops up, why?
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.yar, PID: 2752 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yar/com.yar.ExerciseActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.yar.resourses.ExersiceModelAdapter.getCount(ExersiceModelAdapter.java:27) at android.widget.ListView.setAdapter(ListView.java:480) at com.yar.ExerciseActivity.onCreate(ExerciseActivity.java:38) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) class with adapter:
package com.yar; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import com.yar.resourses.ExersiceModel; import com.yar.resourses.ExersiceModelAdapter; import com.yar.resourses.Requests; import com.yar.serialize.Serialize; import java.util.ArrayList; import java.util.List; public class ExerciseActivity extends AppCompatActivity { private ListView listView; // private static ArrayAdapter<String> adapter; private String currentIntentDay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exercise); currentIntentDay = getIntent().getStringExtra(Requests.DAY_POSITION); Serialize.getExersices(currentIntentDay); listView = (ListView) findViewById(R.id.listView_ExerciseActivity); // создаем адаптер ExersiceModelAdapter adapter = new ExersiceModelAdapter(this, initData() ); Swears on this line:
listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO // здесь передаем название } }); } public void button_Exercise_Add(View view) { Intent intent = new Intent(getApplicationContext(), ListExerciseActivity.class); startActivity(intent); } private List<ExersiceModel> initData() { List<ExersiceModel> list = new ArrayList<>(); list = Serialize.myDaysMap.get(currentIntentDay); return list; } } model:
package com.yar.resourses; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.TextView; import com.yar.R; import java.util.ArrayList; import java.util.List; public class ExersiceModelAdapter extends BaseAdapter { private List<ExersiceModel> list = new ArrayList<>(); private LayoutInflater layoutInflater; public ExersiceModelAdapter(Context context, List<ExersiceModel> list) { this.list = list; layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; if (view == null) { //импорт layout должен быть из своего проекта view = layoutInflater.inflate(R.layout.item_layout, parent, false); } ExersiceModel exersiceModel = getExersiceModel(position); TextView txtExersice = (TextView) view.findViewById(R.id.txt_Exersice); txtExersice.setText(exersiceModel.getExersiceName()); TextView txtApproach = (TextView) view.findViewById(R.id.txt_Approach); txtApproach.setText(String.valueOf(exersiceModel.getApproach())); TextView txtReiteration = (TextView) view.findViewById(R.id.txt_Reiteration); txtReiteration.setText(String.valueOf(exersiceModel.getReiteration())); return view; } private ExersiceModel getExersiceModel(int position) { return (ExersiceModel) getItem(position); } } and here on this
return list.size(); I understand the collection for some reason in the model becomes empty, the question why is this happening?
The task of this activity is to adopt the values stored in another collection, in a different class each time this activity is opened, because the values of the track can vary
Serialize.myDaysMap- what is it? It returnsnull. - Suvitruf ♦finish(), back button,startActivity(...)? - woesssExtra(Requests.DAY_POSITION)to the new intent - hence the problem. To return to the previous activity, you do not need to callstartActivity(...), you just need to close the current one by callingfinish()- woesss