我有需要刷新的列表,我使用PullToRefresh。我曾经使用过Activiti,但现在我需要重写片段中的所有内容。当我使用Activiti一切正常时,但是现在我发现了一个错误,当我复制了我使用相同方法和逻辑的所有内容时,但是发生了一个错误,即nullpointerexception。这是我的片段代码:
private PullToRefreshListView mPullRefreshListView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_vk, container, false);
Map<String, String> networkDetails = getConnectionDetails();
if (networkDetails.isEmpty()) {
Toast.makeText(getActivity().getApplicationContext(),
"Нет интернет подключения!", Toast.LENGTH_LONG).show();
}
getActivity().getActionBar().setDisplayShowTitleEnabled(false);
/** Create an array adapter to populate dropdownlist */
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity().getBaseContext(),
android.R.layout.simple_spinner_dropdown_item, actions);
/** Enabling dropdown list navigation for the action bar */
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
/** Defining Navigation listener */
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition,
long itemId) {
switch (itemPosition) {
case 1:
Intent intent1 = new Intent(getActivity().getApplicationContext(),
KfuNews.class);
getActivity().finish();
startActivity(intent1);
break;
}
return false;
}
};
/**
* Setting dropdown items and item navigation listener for the actionbar
*/
getActivity().getActionBar().setListNavigationCallbacks(adapter, navigationListener);
boolean firstrun = getActivity().getSharedPreferences("PREFERENCE", getActivity().MODE_PRIVATE)
.getBoolean("firstrun", true);
// Set a listener to be invoked when the list should be refreshed.
mPullRefreshListView
.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(
getActivity().getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
// Do work to refresh the list here.
new GetDataTask().execute();
}
});
// Add an end-of-list listener
mPullRefreshListView
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(getActivity().getApplicationContext(), "End of List!",
Toast.LENGTH_SHORT).show();
}
});
sp11 = getActivity().getSharedPreferences("VK", 0);
editor1 = sp11.edit();
account.restore(getActivity());
if ((firstrun) || (account.access_token == null)) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Авторизация")
.setMessage(
"для просмотра новостей необходимо авторизоваться.")
.setCancelable(false)
.setPositiveButton("Авторизоваться",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
editor1.putInt("isVk", 1).commit();
editor1.apply();
startLoginActivity();
}
})
.setNegativeButton("Отмена",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
editor1.putInt("isVk", 0).commit();
editor1.apply();
Intent intent = new Intent(
getActivity().getApplicationContext(),
KfuNews.class);
getActivity().finish();
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
// Save the state
getActivity().getSharedPreferences("PREFERENCE", getActivity().MODE_PRIVATE).edit()
.putBoolean("firstrun", false).commit();
}
if (account.access_token != null) {
api = new Api(account.access_token, Constants.API_ID);
}
Log.d("isChange", isChangedStat + "");
getWall();
return view;
}
我在这一行有问题:
mPullRefreshListView
.setOnRefreshListener(new OnRefreshListener<ListView>() {
我有nullpointerexception,我没有太多时间来解决这个问题,请帮忙。
答案 0 :(得分:0)
在片段中启动listview并将listenlay添加到此列表视图中。
答案 1 :(得分:0)
您没有初始化 mPullRefreshListView 变量因此nullpointerexception。根据您的代码,您缺少这一行:
mPullRefreshListView = (PullToRefreshListView) view .findViewById(R.id.mPullRefreshListView);