Reputation: 1
I having a pager adapter as following
public class MainPagerFragmentAdapter extends FragmentPagerAdapter implements IdInterface {
ContactsFragment contactSearch;
public MainPagerFragmentAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if (contactSearch == null)
contactSearch = new ContactsFragment();
return contactSearch;
}
return null;
}
public void onRawIdAcquired(long arg0) {
Log.d("LOG","id=" + arg0);
}
}
this is my contacts fragment
public class ContactsFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private long rawId;
private SimpleCursorAdapter mAdapter;
public static final String ARG_ID = "RAWID";
public static final String SORT_KEY_PRIMARY = "sort_key";
public static final Uri URI = ContactsContract.Data.CONTENT_URI;
public static final String[] PROJECTION = new String[] {
ContactsContract.Data._ID, ContactsContract.Data.RAW_CONTACT_ID};
public static final String SELECTION = ContactsContract.Data.MIMETYPE
+ " = ? ";
public static final List<String> ARGS;
static {
ARGS = new ArrayList<String>();
ARGS.add(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
}
private ListView mContactList;
private View mContactFragmentView;
private TextView noResultText;
private Button searchOnlineBtn;
IdInterface dataPasser;
public ContactsFragment() {
dataPasser = (IdInterface) null;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mContactFragmentView = inflater.inflate(R.layout.fragment_contacts,
container, false);
mContactList = (ListView) mContactFragmentView
.findViewById(R.id.contacts_list);
String[] fromColumns = { SORT_KEY_PRIMARY };
int[] toViews = { R.id.contactName };
mAdapter = new ContactsCursorAdapter(getActivity(),
R.layout.contact_list_item, null, fromColumns, toViews);
mContactList.setAdapter(mAdapter);
mContactList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Cursor cursor = (Cursor) parent.getAdapter().getItem(position);
rawId = cursor.getLong(cursor
.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
Bundle params = new Bundle();
params.putLong(ARG_ID, rawId);
ContactsFragment fragobj = new ContactsFragment();
fragobj.setArguments(params);
Log.v("", "fragobj=" + params);
try {
dataPasser.onRawIdAcquired(rawId);
} catch (Exception e) {
Log.v("", "error=" + e);
}
}
});
getLoaderManager().initLoader(0, null, this);
return mContactFragmentView;
}
}
and lastly Interface
public interface IdInterface {
public void onRawIdAcquired(long rawId);
}
when i click on the item, it catches java.lang.nullpointerexception What i should do in order to let the MainPagerFragmentAdapter to obtain the Id when any of the contact id being clicked?
Noted: ContactsFragment is a library project, thus i am using interface to passing value among project
Upvotes: 0
Views: 1361
Reputation: 1
Finally got it solved, initialize the dataPasser as following in the Fragment
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
context = getActivity();
dataPasser =(IdInterface)context;
}
The Interface should implements on MainActivity which call the FragmentAdapter instead of the FragmentAdapter.
Upvotes: 0
Reputation: 146
In yourreturn getItem Function you are always returning null unless you ask for position 0 Then you return a ContactsFragment.
This function is called on item click so I guess this is where you'll find your problem.
public Fragment getItem(int position) {
switch (position) {
case 0:
if (contactSearch == null)
contactSearch = new ContactsFragment();
return contactSearch;
}
return null; <--- should be the problem
}
You should fix this function to actually return a fragment. According to the other answers I'd suggest to have a closer look on your code. Looks like there are some more obvious problems.
To initialize your dataPasser you should first implement it as its a interface. Try by replacing
dataPasser = (IdInterface) null;
with
dataPasser = new IdInterface {
public void onRawIdAcquired(long rawId){
//your code
}
};
Or create a new class implementing this interface and instantiate it here instead.
Upvotes: 0
Reputation: 9326
In your constructor you put dataPasser
to null:
IdInterface dataPasser;
public ContactsFragment() {
dataPasser = (IdInterface) null; // <- Here
}
But I don't see code where you initialize it, so dataPasser.onRawIdAcquired(rawId);
causes a NullPointerException
because your dataPasser
is null.
Either initialize the dataPasser
somewhere, or if it's already supposed to do this and it isn't for some reason, use this instead:
if(dataPasser != null)
dataPasser.onRawIdAcquired(rawId);
Upvotes: 0
Reputation: 152787
Your dataPasser
is null
.
Either initialize it to a non-null object that implements the interface, or check for != null
before trying to call a method on it.
Upvotes: 1