当wifi打开时 - 一切正常,但当它关闭时 - 我得到null指针异常
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
问题是您正在onCreateView()
中创建ListView并在onCreate()
中设置其adapter
。
您应该知道onCreate()
总是在onCreateView()
之前调用,到那时您的listview
还没有被创建..这就是您NullPointerException
的原因。
将您的onCreate()
代码放在onCreateView()
问题是您在同一活动中调用任何其他布局文件时,正在另一个布局文件中创建 ListView。因此,它在调用活动的布局不匹配。因此,将 ListView 放置在您的活动使用的相同布局中,或者如果您希望 ListView 在另一个布局中,请更改 ArrayAdapter 对象上下文,例如:将其更改为
ArrayAdapteradapter = new ArrayAdapter(this, R.布局., R.id., (;
有了这个:
ArrayAdapteradapter = new ArrayAdapter(, R.布局., R.id., (;
只需将列表视图代码放在回调onViewCreated
试试这个代码:
Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
return inflater.inflate(R.layout.fragment_news, container, false);
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
listview = (ListView) view.findViewById(R.id.listview1);
try
{
// put your codes here
} catch (Exception e)
{
e.printStackTrace();
}
}
使用 onActivityCreated(Bundle savedInstanceState( 覆盖的方法,并尝试将代码放入其中。这将有助于修复此崩溃。因为在视图存在之前创建视图会产生此异常并使应用程序崩溃。每当从此视图移动时,使用 onPause(( 和 onResume(( 也可以减少崩溃。
您的问题是您已经在onCreateView()
方法上创建了ListView
,并且已经在onCreate()
中设置了适配器。所以onCreate()
必须在onCreateView()
之前调用,这就是为什么ListViews
没有初始化,并且你发现了空指针异常。