我遇到了問題。每當我的應用嘗試打開一個片段時,我都會收到一個nullpointerexception。我認爲我的適配器沒有在我的片段中找到視圖。arrayadapter中的Nullpointerexception。沒有找到視圖?
以下是錯誤:
11-24 13:00:27.429: E/AndroidRuntime(31304): FATAL EXCEPTION: main
11-24 13:00:27.429: E/AndroidRuntime(31304): java.lang.NullPointerException
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.laurenswuyts.witpa.ListEventAdapter.<init>(ListEventAdapter.java:23)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.laurenswuyts.witpa.EventFragment.parseEventFromFQLResponse(EventFragment.java:154)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.laurenswuyts.witpa.EventFragment$2.onCompleted(EventFragment.java:119)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.facebook.Request$4.run(Request.java:1669)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.os.Handler.handleCallback(Handler.java:615)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.os.Handler.dispatchMessage(Handler.java:92)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.os.Looper.loop(Looper.java:137)
11-24 13:00:27.429: E/AndroidRuntime(31304): at android.app.ActivityThread.main(ActivityThread.java:4944)
11-24 13:00:27.429: E/AndroidRuntime(31304): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 13:00:27.429: E/AndroidRuntime(31304): at java.lang.reflect.Method.invoke(Method.java:511)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
11-24 13:00:27.429: E/AndroidRuntime(31304): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
11-24 13:00:27.429: E/AndroidRuntime(31304): at dalvik.system.NativeStart.main(Native Method)
這裏是我的mainactivity:
public class EventFragment extends Fragment {
//Arraylist with event details
private static List<EventDetails> dataEvent;
//Adapter to put event details in listview
ListEventAdapter eventAdapter;
//Listview with the events
ListView lvEvents;
//To show Progress Circle
LinearLayout linearProgress;
private static final String TAG = "EventFragment";
private static final int REAUTH_ACTIVITY_CODE = 100;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.event,
container, false);
linearProgress = (LinearLayout) view.findViewById(R.id.linProgress);
lvEvents = (ListView) view.findViewById(R.id.lvEvent);
// Check for an open session
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// Get the user's data
ShowEventsFromFQL();
}
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
public void ShowEventsFromFQL(){
// Check for an open session
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
//Progressbar active
linearProgress.setVisibility(View.VISIBLE);
// Get the user's data
// ORDER BY attending_count DESC
String fqlQuery = ".....";
Bundle params = new Bundle();
params.putString("q", fqlQuery);
Session session2 = Session.getActiveSession();
Request request = new Request(session2,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
Log.i(TAG, "Result: " + response.toString());
parseEventFromFQLResponse(response);
}
});
Request.executeBatchAsync(request);
}
}
public final void parseEventFromFQLResponse(Response response)
{
dataEvent = new ArrayList<EventDetails>();
try
{
GraphObject graphObj = response.getGraphObject();
JSONObject jsonObj = graphObj.getInnerJSONObject();
JSONArray jsonArr = jsonObj.getJSONArray("data");
for (int i = 0; i < (jsonArr.length()); i++)
{
JSONObject jObject = jsonArr.getJSONObject(i);
int attending_count = jObject.getInt("attending_count");
String name = jObject.getString("name");
String host = jObject.getString("host");
String description = jObject.getString("description");
String location = jObject.getString("location");
String start_time = jObject.getString("start_time");
String end_time = jObject.getString("end_time");
dataEvent.add(new EventDetails(attending_count, name, host, description, location, start_time, end_time));
}
// if(dataEvent != null){
eventAdapter = new ListEventAdapter(getActivity(), R.layout.event_item, dataEvent);
lvEvents.setAdapter(eventAdapter);
eventAdapter.notifyDataSetChanged();
//Progressbar gone
linearProgress.setVisibility(View.GONE);
// }
}
catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REAUTH_ACTIVITY_CODE) {
uiHelper.onActivityResult(requestCode, resultCode, data);
}
}
private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
if (session != null && session.isOpened()) {
// Get the user's data.
ShowEventsFromFQL();
}
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(final Session session, final SessionState state, final Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
uiHelper.onSaveInstanceState(bundle);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
}
這裏是我的適配器:
public class ListEventAdapter extends ArrayAdapter<EventDetails> {
public ListEventAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<EventDetails> eItems;
public ListEventAdapter(Context context, int resource, List<EventDetails> eItems) {
super(context, resource, eItems);
this.eItems = eItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
TextView txtAttending_count = null;
TextView txtName = null;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.event_item, null);
}
txtAttending_count = (TextView) v.findViewById(R.id.attending_count);
txtName = (TextView) v.findViewById(R.id.name);
EventDetails p = eItems.get(position);
if (p != null) {
if (txtAttending_count != null) {
txtAttending_count.setText(""+p.getAttending_count());
}
if (txtName != null) {
txtName.setText(""+p.getName());
}
}
return v;
}
}
任何人可以幫我,因爲我真不」不知道問題是什麼。
問題是'getActivity()'在方法'parseEventFromFQLResponse()'中返回null。如果活動尚未附加到片段,或者已被分離,則可能發生這種情況。看看http://stackoverflow.com/q/6215239/827110 –