gpt4 book ai didi

java - Android 使用 java nullpointerException 接口(interface)传递值

转载 作者:行者123 更新时间:2023-12-01 12:37:58 25 4
gpt4 key购买 nike

我有一个寻呼机适配器如下

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);
}

}

这是我的联系人 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;
}

}

最后是界面

public interface IdInterface {
public void onRawIdAcquired(long rawId);

}

当我单击该项目时,它捕获 java.lang.nullpointerException我应该做什么才能让 MainPagerFragmentAdapter 在单击任何联系人 id 时获取 Id?

注意:ContactsFragment 是一个库项目,因此我使用接口(interface)在项目之间传递值

最佳答案

您的dataPassernull

将其初始化为实现该接口(interface)的非 null 对象,或者在尝试调用其方法之前检查 != null

关于java - Android 使用 java nullpointerException 接口(interface)传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25398773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com