The code below is the class(Checklist.java) I'm trying to open when checklistbutton is clicked:-
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
public class Checklist extends AppCompatActivity {
// ExpandableListAdapter listAdapter;
ExpListViewAdapterWithCheckbox listAdapter;
ExpandableListView expListView;
ArrayList<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpListViewAdapterWithCheckbox(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Toiletries");
listDataHeader.add("Clothes");
listDataHeader.add("Essentials");
// Adding child data
List<String> toiletries = new ArrayList<String>();
toiletries.add("Bandages");
toiletries.add("Contacts");
toiletries.add("Contacts Solution");
toiletries.add("Cologne");
toiletries.add("Conditioner");
toiletries.add("Cotton Buds");
toiletries.add("Deodorant");
toiletries.add("Hairbrush");
toiletries.add("Nail Clippers");
toiletries.add("Razor");
toiletries.add("Shampoo");
toiletries.add("Shaving Gel");
toiletries.add("Toothbrush");
toiletries.add("Toothpaste");
List<String> clothes = new ArrayList<String>();
clothes.add("Belt ");
clothes.add("Bras");
clothes.add("Casual Pants");
clothes.add("Casual Shirts");
clothes.add("Heavy Coat");
clothes.add("Jumper");
clothes.add("Light Jacket");
clothes.add("Pyjamas");
clothes.add("Scarf");
clothes.add("Shoes");
clothes.add("Shorts");
clothes.add("Socks");
clothes.add("Swimwear");
List<String> essentials = new ArrayList<String>();
essentials.add("Digital Camera");
essentials.add("Headache Pills");
essentials.add("Fever Pills");
essentials.add("Diarrhea Pills");
essentials.add("Flu Pills");
essentials.add("Cough Medicine");
essentials.add("Powerbank");
essentials.add("USB Power Socket");
essentials.add("Sunglasses");
essentials.add("Earphones");
listDataChild.put(listDataHeader.get(0), toiletries); // Header, Child data
listDataChild.put(listDataHeader.get(1), clothes);
listDataChild.put(listDataHeader.get(2), essentials);
}
}
The code below is where the checklistbutton is stored (ContentAdapter.java) :-
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Calendar;
import java.util.List;
import android.util.Log;
import android.widget.Toast;
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder>{
private final Drawable[] mPlacePictures;
List<Flights> mainList;
Context context;
public ContentAdapter(Context context , List<Flights> mainList) {
this.context = context;
this.mainList = mainList;
//instantiate resources for images
Resources resources = context.getResources();
//gets images from drawable and recycles images
TypedArray a = resources.obtainTypedArray(R.array.places_picture);
mPlacePictures = new Drawable[a.length()];
for (int i = 0; i < mPlacePictures.length; i++) {
mPlacePictures[i] = a.getDrawable(i);
}
a.recycle();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Flights flight = mainList.get(position);
holder.picture.setImageDrawable(mPlacePictures[position % mPlacePictures.length]);
holder.DestinationTo.setText(String.valueOf(flight.getDestinationto()));
holder.Date.setText("Departs on: " + String.valueOf(flight.getDate()));
holder.Time.setText("Boarding time: " + String.valueOf(flight.getTime()));
}
@Override
public int getItemCount() {
Log.i("sss size",mainList.size()+"");
return mainList.size();
}
//removes item from list
public void removeItem(int position){
mainList.remove(position);
notifyItemRemoved(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView picture;
public TextView DestinationTo;
public TextView Date;
public TextView Time;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.homepage_item_card, parent, false));
picture = (ImageView) itemView.findViewById(R.id.card_image);
DestinationTo = (TextView) itemView.findViewById(R.id.card_title);
Date = (TextView) itemView.findViewById(R.id.card_text2);
Time = (TextView) itemView.findViewById(R.id.card_text);
//view details button
Button button = (Button) itemView.findViewById(R.id.action_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, DetailActivity.class);
intent.putExtra("Position", getAdapterPosition());
context.startActivity(intent);
}
});
//delete button
final ImageButton deletebutton = (ImageButton) itemView.findViewById(R.id.delete_button);
deletebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onShowQuitDialog();
}
});
//edit button
final ImageButton editbutton = (ImageButton) itemView.findViewById(R.id.edit_button);
editbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("Position", getAdapterPosition());
context.startActivity(intent);
}
});
//open checklist button
final ImageButton checklistbutton = (ImageButton) itemView.findViewById(R.id.checklist_button);
checklistbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, Checklist.class);
intent.putExtra("Position", getAdapterPosition());
context.startActivity(intent);
}
});
}
//shows dialog to double confirm with user whether they really want to delete record
public void onShowQuitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false);
builder.setMessage("Are you sure you want to delete this flight record?");
builder.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
DatabaseHelper db = new DatabaseHelper(context);
db.deleteFlight(mainList.get(getAdapterPosition()));
removeItem(getAdapterPosition());
db.close();
Toast.makeText(context, "Flight deleted!", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show();
}
}
}
Finally, the Android.xml file which I think is where the problem should be fixed but do not know where or what to modify to make the checklistbutton work :-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="project.myapplication">
<application
android:allowBackup="true"
android:icon="@drawable/aeroplan"
android:label="@string/app_name"
android:roundIcon="@drawable/aeroplan"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Main"
android:label="@string/app_name"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailActivity"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".Main">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Main" />
</activity>
<activity
android:name=".UpdateActivity"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".Main">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Main" />
</activity>
<activity android:name=".Checklist"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
To all the gurus out there, I did the Checklist feature on a seperate project and it was working fine before I moved everything carefully into my main project as an added feature. There were no error codes that was shown.
Below is the logcat from right before the app crashes until after clicking the checklistbutton :-
--------- beginning of crash
06-20 02:40:46.453 8487-8487/project.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: project.myapplication, PID: 8487
java.lang.RuntimeException: Unable to start activity ComponentInfo{project.myapplication/project.myapplication.Checklist}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
at project.myapplication.Checklist.onCreate(Checklist.java:39)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)