I have a ViewPager with fragments, and one of the Fragment "F" shows a ListView "ListF", with a list of categories.
This "ListF" has the ability to launch an activity "A" with another ListView "ListA" of details (Which it'd be the items of the category selected).
Then this activity has the option to launch a second activity "B" that will display the information for each item of the ListView "ListA" from activity "A"(The information of the item selected).
All the information is parsed from an xml, info for the Fragment "F", activity "A", and activity "B".
The problem that I'm having is when I try to navigate back from activity "B" to activity "A", I get a NullPointerException, stating that one of the values I passed through a bundle (from Fragment "F" to Activity "A" is null) or sort of like that.
This is my Fragment
public class DirectoryFragment extends Fragment{
DirectoryAdapter adapter;
ListView categoriesList;
Context context;
ArrayList<Category> categories=new ArrayList<Category>();
String tag="directory";
Category category;
JSONManager jmanager;
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
View view=inflater.inflate(R.layout.directory_activity, container,false);
categoriesList=(ListView)view.findViewById(R.id.categorieslist);
return view;
}
public DirectoryFragment(){}
public DirectoryFragment(Context context1){
context=context1;
jmanager=new JSONManager(getFragment(),tag);
jmanager.execute();
}
public DirectoryFragment getFragment(){
return this;
}
public void onAttach(Activity activity){
super.onAttach(activity);
}
/*This is the method that displays the listview, I call it from the class that does all the parsing, since I'm using AsyncTask */
public void displayBD(final ArrayList<Category> categories){
try {
if(categories==null || categories.size()==0){
}
} catch (Exception e) {
e.printStackTrace();
}
adapter=new DirectoryAdapter(context,categories);
categoriesList.setAdapter(adapter);
categoriesList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(android.widget.AdapterView<?> parent,
View view, int position, long id) {
category=categories.get(position);
Bundle bundle=new Bundle();
bundle.putString("title", category.getTitle());
bundle.putString("cid",category.getCid());
Intent intent=new Intent(context,ListingsBD.class);
intent.putExtras(bundle);
startActivityForResult(intent,1); ///launching Activity "A"
}
});
}
}
This is the Activity "A"
public class ListingsBD extends Activity{
ListingsBDAdapter adapter;
ListView listingList;
ArrayList<Business> listings=new ArrayList<Business>();
Context context;
String title=null,cid=null;
JSONManager jmanager;
Business listing;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context=this;
savedInstanceState=this.getIntent().getExtras();
setContentView(R.layout.listings_bd_layout);
android.app.ActionBar actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
listingList=(ListView)findViewById(R.id.listingsBD);
cid=savedInstanceState.getString("cid");// this is the line where I get the NPE
title=savedInstanceState.getString("title");
jmanager=new JSONManager(getFragment(),"listingsbd",cid);
jmanager.execute();
}
public ListingsBD getFragment(){
return this;
}
public void onPause(){
super.onPause();
}
public void displayListings(final ArrayList<Business> listings){
try {
if(listings==null || listings.size()==0){
//Retrieve again the data list
}
} catch (Exception e) {
e.printStackTrace();
}
adapter=new ListingsBDAdapter(context,listings);
listingList.setAdapter(adapter);
listingList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(android.widget.AdapterView<?> parent,
View view, int position, long id) {
listing=listings.get(position);
Bundle bundle=new Bundle();
bundle.putString("name", listing.getName());
bundle.putString("address", listing.getAddress());
bundle.putString("zipCode",listing.getZipCode());
bundle.putString("city", listing.getCity());
bundle.putString("state", listing.getState());
bundle.putString("country", listing.getCountry());
bundle.putString("phone", listing.getPhone());
Intent intent=new Intent(context,SingleListing.class);
intent.putExtras(bundle);
startActivityForResult(intent,1);
}
});
}
}
And the Activity "B"
public class SingleListing extends Activity{
String name=null;
String address=null;
String city=null;
String state=null;
String zipCode=null;
String country=null;
String phone=null;
String imgUrl=null;
TextView nameView,addressView,cityView,phoneView,mobileView,faxView,homeView,tollFreeView,emailView,websiteView;
ImageView image;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_listing_bd_layout);
android.app.ActionBar actionBar=getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
setUp(savedInstanceState);
}
public void setUp(Bundle savedInstanceState){
nameView=(TextView)findViewById(R.id.bd_title);
addressView=(TextView)findViewById(R.id.bd_address);
cityView=(TextView)findViewById(R.id.bd_city);
phoneView=(TextView)findViewById(R.id.bd_phone);
savedInstanceState=this.getIntent().getExtras();
name=savedInstanceState.getString("name");
address=savedInstanceState.getString("address");
city=savedInstanceState.getString("city");
state=savedInstanceState.getString("state");
zipCode=savedInstanceState.getString("zipCode");
country=savedInstanceState.getString("country");
phone=savedInstanceState.getString("phone");;
imgUrl=savedInstanceState.getString("imgUrl");
nameView.setText(name);
if(address.equals(""))
addressView.setVisibility(View.GONE);
else
addressView.setText("Address: "+address);
if(city.equals(""))
addressView.setVisibility(View.GONE);
else
cityView.setText(city+", "+state+", "+zipCode);
if(phone.equals(""))
phoneView.setVisibility(View.GONE);
else
phoneView.setText("Phone: "+phone);
if(mobile.equals(""))
mobileView.setVisibility(View.GONE);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
This is my Log
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test23/com.example.test23.ListingsBD}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2153)
at android.app.ActivityThread.access$700(ActivityThread.java:137)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5031)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.test23.ListingsBD.onCreate(ListingsBD.java:36)
at android.app.Activity.performCreate(Activity.java:5058)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2079)
... 11 more
UPDATE: This only happens when I press the back button from the action bar, If I press back from the bottom menu, Activity "A" is fully displayed and no exception is thrown. Any Help is appreciated, if you need more code or any example please let me know. Thanks!