Reputation: 55
Inside my UpdateSupervitionActivity
class CallAdapter()
method is called to generate the listview
. The listview
has a button for each item
to remove an item
. After the listview
is loaded, when I try to remove an item from the listview
by clicking the listview button, it throws a nullpointer
exception and the app crashes.
Java class with CallAdapter() method:
package bd.edu.bubt.regup;
public class UpdateSupervitionActivity extends AppCompatActivity {
EditText fac_code, s_intake, s_section;
ListView listView;
Button search, addlist, update;
Spinner shift;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_supervition);
fac_code = (EditText) findViewById(R.id.sh_code);
s_intake = (EditText) findViewById(R.id.intake);
s_section = (EditText) findViewById(R.id.section);
listView = (ListView) findViewById(R.id.faclistview);
shift = (Spinner) findViewById(R.id.shift);
addlist = (Button) findViewById(R.id.addlist);
search = (Button) findViewById(R.id.search);
update = (Button) findViewById(R.id.update);
}
public void CallAdapter(Context context)
{
adapterlist.clear();
if(daylist.size() == 0)
{
//no intake list for day shift
if(evelist.size() == 0)
{
//no intake list for evening shift
}
else if(evelist.size() > 0)
{
for (int i=0;i<evelist.size();i++)
{
String s = evelist.get(i);
int p = s.indexOf("-");
String intake = s.substring(0,p);
String section = s.substring(p+1,s.length());
adapterlist.add(new UpdateSupervisionItem(intake, section, "Evening"));
}
}
}
else if(daylist.size() > 0)
{
for (int i=0;i<daylist.size();i++)
{
String s = daylist.get(i);
int p = s.indexOf("-");
String intake = s.substring(0,p);
String section = s.substring(p+1,s.length());
adapterlist.add(new UpdateSupervisionItem(intake, section, "Day"));
}
if(evelist.size() == 0)
{
//no intake list for evening shift
}
else if(evelist.size() > 0)
{
for (int i=0;i<evelist.size();i++)
{
String s = evelist.get(i);
int p = s.indexOf("-");
String intake = s.substring(0,p);
String section = s.substring(p+1,s.length());
adapterlist.add(new UpdateSupervisionItem(intake, section, "Evening"));
}
}
}
UpdateSupervisionAdapter updateSupervisionAdapter = new UpdateSupervisionAdapter(context, R.layout.update_supervision_list_view_layout, adapterlist);
listView.setAdapter(updateSupervisionAdapter);
}
}
Adapter class:
package bd.edu.bubt.regup;
public class UpdateSupervisionAdapter extends ArrayAdapter<UpdateSupervisionItem> {
ArrayList<UpdateSupervisionItem> adapterlist = new ArrayList<>();
Context context;
public UpdateSupervisionAdapter(Context context, int textViewResourceId, ArrayList<UpdateSupervisionItem> objects) {
super(context, textViewResourceId, objects);
this.context = context;
adapterlist = objects;
}
@Override
public int getCount() {
return super.getCount();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.update_supervision_list_view_layout, null);
TextView intake = (TextView) v.findViewById(R.id.intake);
TextView section = (TextView) v.findViewById(R.id.section);
TextView shift = (TextView) v.findViewById(R.id.shift);
final Button remove = (Button) v.findViewById(R.id.remove);
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.blink_anim);
remove.startAnimation(animation);
UpdateSupervisionItem updateSupervisionItem = getItem(position);
String in_take = updateSupervisionItem.getIntake();
String sec_tion = updateSupervisionItem.getSection();
String shi_ft = updateSupervisionItem.getShift();
UpdateSupervitionActivity updateSupervitionActivity = new UpdateSupervitionActivity();
if(TextUtils.equals(shi_ft,"Day"))
{
updateSupervitionActivity.daylist.remove(in_take+ "-" +sec_tion);
updateSupervitionActivity.CallAdapter(context);
}
else if(TextUtils.equals(shi_ft,"Evening"))
{
updateSupervitionActivity.evelist.remove(in_take+ "-" +sec_tion);
updateSupervitionActivity.CallAdapter(context);
}
}
});
intake.setText("Intake: " +adapterlist.get(position).getIntake());
section.setText("Section: " +adapterlist.get(position).getSection());
shift.setText("Shift: " +adapterlist.get(position).getShift());
return v;
}
}
Error log:
FATAL EXCEPTION: main
Process: bd.edu.bubt.regup, PID: 31219
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at bd.edu.bubt.regup.UpdateSupervitionActivity.CallAdapter(UpdateSupervitionActivity.java:352)
at bd.edu.bubt.regup.UpdateSupervisionAdapter$1.onClick(UpdateSupervisionAdapter.java:70)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Upvotes: 0
Views: 198
Reputation: 491
The reason why you get this error is that listview is not found by id. It seems like your adapter is first initialized from another activity, which is the real context for your layout with listview. There are two ways of solving this: 1) You should initialize your listview in update class through
ListView listView = context.findViewById(R.id.faclistview)
2) You can move your CallAdapter into actual context activity for your adapter
UPDATE try that:
ListView listView = (ListView) ((ActivityWhereAdapterIsForstInitialized) context).findViewById(R.id.faclistview)
Upvotes: 1