当wifi打开时-一切正常,但是当它关闭时-我得到nullPointer异常
package ua.khuta.myskidka;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.database.Cursor;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class FragmentNews extends Fragment {
private View v;
ListView lvNews;
Database db;
mySCadapter scAdapter;
public String DEBUG_TAG = "myLogs";
String[] from = new String[] { Database.NEWS_TITLE, Database.NEWS_IMAGE_URL,
Database.NEWS_SHORT_DESCRIPTION };
int [] to = new int[] { R.id.item_news_tv_title, R.id.item_news_iv_image,
R.id.item_news_tv_short_description };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_news, null);
lvNews = (ListView) v.findViewById(R.id.lv_new);
lvNews.setCacheColorHint(Color.TRANSPARENT);
return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = Database.getInstance(getActivity());
ConnectivityManager connManager = (ConnectivityManager) getActivity()
.getSystemService(getActivity().CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
Log.d(DEBUG_TAG, "wi-fi is on");
db.cleanDB();
GetJSON getJSON = new GetJSON();
getJSON.execute();
} else {
Log.d(DEBUG_TAG, "wi-fi is off");
Cursor c = db.getNewsFromDb();
Log.d(DEBUG_TAG, String.valueOf(c.getCount()));
mySCadapter scAdapter = new mySCadapter(getActivity(), R.layout.item_news, c,
from, to);
lvNews.setAdapter(scAdapter);
}
}
class GetJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Creating JSON Parser instance
return null;
}
@Override
protected void onPostExecute(Void result) {
Cursor c = db.getNewsFromDb();
scAdapter = new mySCadapter(getActivity(), R.layout.item_news, c,
from, to);
lvNews.setAdapter(scAdapter);
super.onPostExecute(result);
}
}
}
错误日志
10-10 09:12:53.275: E/AndroidRuntime(28582): FATAL EXCEPTION: main
10-10 09:12:53.275: E/AndroidRuntime(28582): java.lang.RuntimeException: Unable to start activity ComponentInfo{ua.khuta.myskidka/ua.khuta.myskidka.MainActivity}: java.lang.NullPointerException
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.os.Looper.loop(Looper.java:137)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-10 09:12:53.275: E/AndroidRuntime(28582): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 09:12:53.275: E/AndroidRuntime(28582): at java.lang.reflect.Method.invoke(Method.java:525)
10-10 09:12:53.275: E/AndroidRuntime(28582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-10 09:12:53.275: E/AndroidRuntime(28582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 09:12:53.275: E/AndroidRuntime(28582): at dalvik.system.NativeStart.main(Native Method)
10-10 09:12:53.275: E/AndroidRuntime(28582): Caused by: java.lang.NullPointerException
10-10 09:12:53.275: E/AndroidRuntime(28582): at ua.khuta.myskidka.FragmentNews.onCreate(FragmentNews.java:82)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:556)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.Activity.performStart(Activity.java:5143)
10-10 09:12:53.275: E/AndroidRuntime(28582): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
10-10 09:12:53.275: E/AndroidRuntime(28582): ... 11 more