设置我的适配器时,我一直收到Null Pointer Exceptions。我有一个包含多个片段的ViewPager。我已经搜索过并尽力而为。本质上,希望将适配器附加到ViewPager可以使用的片段上的列表视图。
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.parse.FindCallback;
import com.parse.ParseACL;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import java.util.ArrayList;
import java.util.List;
public class MyFragment extends Fragment implements AdapterView.OnItemClickListener {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";
private EditText mTaskInput;
private ListView mListView;
private TaskAdapter mAdapter = null;
private Button mButton;
public static final MyFragment newInstance(String message)
{
MyFragment f = new MyFragment();
Bundle bdl = new Bundle(1);
bdl.putString(EXTRA_MESSAGE, message);
f.setArguments(bdl);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String message = getArguments().getString(EXTRA_MESSAGE);
View v = inflater.inflate(R.layout.myfragment_layout, container, false);
TextView messageTextView = (TextView)v.findViewById(R.id.textView);
messageTextView.setText(message);
mAdapter = new TaskAdapter(getActivity(), new ArrayList<Task>());
mTaskInput = (EditText) getActivity().findViewById(R.id.task_input);
mListView = (ListView) getActivity().findViewById(R.id.task_list);
mButton = (Button) getActivity().findViewById(R.id.submit_button);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
updateData();
return v;
}
public void updateData(){
ParseQuery<Task> query = ParseQuery.getQuery(Task.class);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.setCachePolicy(ParseQuery.CachePolicy.CACHE_THEN_NETWORK);
query.findInBackground(new FindCallback<Task>() {
@Override
public void done(List<Task> tasks, ParseException error) {
if(tasks != null){
mAdapter.clear();
for (int i = 0; i < tasks.size(); i++) {
mAdapter.add(tasks.get(i));
}
}
}
});
}
public void createTask(View v) {
if (mTaskInput.getText().length() > 0){
Task t = new Task();
t.setACL(new ParseACL(ParseUser.getCurrentUser()));
t.setUser(ParseUser.getCurrentUser());
t.setDescription(mTaskInput.getText().toString());
t.setCompleted(false);
t.saveEventually();
mAdapter.insert(t, 0);
mTaskInput.setText("");
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Task task = mAdapter.getItem(position);
TextView taskDescription = (TextView) view.findViewById(R.id.task_description);
task.setCompleted(!task.isCompleted());
if(task.isCompleted()){
taskDescription.setPaintFlags(taskDescription.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}else{
taskDescription.setPaintFlags(taskDescription.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
task.saveEventually();
}
}
答案 0 :(得分:1)
你的ListView
在片段中,而不在活动中(根据你的陈述)。因此,使用这一行:
mListView = (ListView) getActivity().findViewById(R.id.task_list);
将尝试从您的活动中获取参考。你应该这样做:
mListView = (ListView) v.findViewById(R.id.task_list);
否则,它将编译(R.id
存在,但不存在于活动中)。请注意更改:
v.findViewById
这会创建对膨胀片段的引用,而不是活动。