我有这个错误让我烦恼。我正在使用NavDrawer,我有两个项目,Home和Admin。
每次我从Home切换到Admin并再次切换回Home时,我会得到一个nullpointer异常。我不知道如何解决它,但我认为它可能涉及onAttach()?
我感谢任何帮助。
package com.example.slidingmenu;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import services.JSONRequest;
import model.Post;
import adapter.PostAdapter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class HomeFragment extends Fragment {
private ListView mainListView;
private MyRequestReceiver receiver;
public HomeFragment() {}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedIntanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mainListView = (ListView) rootView.findViewById(R.id.mainListView);
IntentFilter filter = new IntentFilter(MyRequestReceiver.PROCESS_RESPONSE);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MyRequestReceiver();
getActivity().getApplicationContext().registerReceiver(receiver, filter);
new RequestItemsServiceTask().execute();
return rootView;
}
private class RequestItemsServiceTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog =
new ProgressDialog(getActivity());
@Override
protected void onPreExecute() {
dialog.setMessage("Please wait");
dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
getPostInfo();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
if(dialog.isShowing()) {
dialog.dismiss();
}
}
}
private void getPostInfo() {
boolean internet = isNetworkAvailable(this.getActivity());
if(internet) {
Intent msgIntent = new Intent(getActivity(), JSONRequest.class);
msgIntent.putExtra(JSONRequest.IN_MSG, "getPostInfo");
getActivity().startService(msgIntent);
} else {
Log.d("WHAT?", "What just happened?");
}
}
@Override
public void onResume() {
super.onResume();
}
private void displayPostInfo(String response) {
JSONObject responseObj = null;
try {
responseObj = new JSONObject(response);
boolean success = responseObj.getBoolean("success");
if(success) {
Gson gson = new Gson();
JSONArray postInfo = responseObj.getJSONArray("postInfo");
JSONObject JSONObj = postInfo.toJSONObject(postInfo);
ArrayList<Post> postList = new ArrayList<Post>();
for (int i = 0; i < JSONObj.length() ; i++) {
String str = postInfo.getString(i) + "";
Post post = gson.fromJson(str, Post.class);
postList.add(new Post(post.getPostId(), post.getPostTitle(), post.getPostContent()));
}
PostAdapter adapter = new PostAdapter(this.getActivity(), R.layout.listview_item_row, postList);
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header = (View)inflater.inflate(R.layout.listview_header_row, null);
mainListView.addHeaderView(header);
mainListView.setAdapter(adapter);
} else {
}
} catch(JSONException e) {
e.printStackTrace();
}
}
private boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivity =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if(info != null) {
for(int i = 0; i < info.length; i++) {
Log.w("INTERNET:", String.valueOf(i));
if(info[i].getState() == NetworkInfo.State.CONNECTED) {
Log.w("INTERNET:", "connected!");
return true;
}
}
}
}
return false;
}
public class MyRequestReceiver extends BroadcastReceiver {
public static final String PROCESS_RESPONSE = "com.services.intent.action.PROCESS_RESPONSE";
@Override
public void onReceive(Context context, Intent intent) {
String response = null;
String responseType = intent.getStringExtra(JSONRequest.IN_MSG);
if(responseType.trim().equalsIgnoreCase("getPostInfo")) {
response = intent.getStringExtra(JSONRequest.OUT_MSG);
Log.d("Response", response);
displayPostInfo(response);
}
}
}
}
以下是错误日志:
03-12 13:46:47.679: E/AndroidRuntime(1788): FATAL EXCEPTION: main
03-12 13:46:47.679: E/AndroidRuntime(1788): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.services.intent.action.PROCESS_RESPONSE cat=[android.intent.category.DEFAULT] flg=0x10 (has extras) } in com.example.slidingmenu.HomeFragment$MyRequestReceiver@4172f958
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773)
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.os.Handler.handleCallback(Handler.java:730)
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.os.Looper.loop(Looper.java:137)
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 13:46:47.679: E/AndroidRuntime(1788): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 13:46:47.679: E/AndroidRuntime(1788): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 13:46:47.679: E/AndroidRuntime(1788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 13:46:47.679: E/AndroidRuntime(1788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 13:46:47.679: E/AndroidRuntime(1788): at dalvik.system.NativeStart.main(Native Method)
03-12 13:46:47.679: E/AndroidRuntime(1788): Caused by: java.lang.NullPointerException
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
03-12 13:46:47.679: E/AndroidRuntime(1788): at adapter.PostAdapter.<init>(PostAdapter.java:22)
03-12 13:46:47.679: E/AndroidRuntime(1788): at com.example.slidingmenu.HomeFragment.displayPostInfo(HomeFragment.java:119)
03-12 13:46:47.679: E/AndroidRuntime(1788): at com.example.slidingmenu.HomeFragment.access$1(HomeFragment.java:99)
03-12 13:46:47.679: E/AndroidRuntime(1788): at com.example.slidingmenu.HomeFragment$MyRequestReceiver.onReceive(HomeFragment.java:163)
03-12 13:46:47.679: E/AndroidRuntime(1788): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
03-12 13:46:47.679: E/AndroidRuntime(1788): ... 9 more
答案 0 :(得分:0)
这一行:
if(responseType.trim().equalsIgnoreCase("getPostInfo"))
可能始终为false,因此response
初始化为null
。