0

当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
4

5 回答 5

5

问题是您正在创建 ListViewonCreateView()并将其设置adapteronCreate()..

你应该知道它onCreate()总是在之前被调用onCreateView(),到那时你listview还没有被创建..这就是你得到的原因NullPointerException

将您的代码onCreate()放入onCreateView()

于 2013-10-10T06:36:50.827 回答
0

使用 onActivityCreated(Bundle savedInstanceState) 覆盖方法并尝试将您的代码放入其中。这将有助于修复此崩溃。因为在视图存在之前创建视图会产生此异常并使您的应用程序崩溃。每当您从该视图移动时使用 onPause() 和 onResume() 也会减少崩溃。

于 2014-05-29T12:09:19.077 回答
0

你的问题是你已经创建了你的ListViewononCreateView()方法,并且你已经在onCreate(). 所以onCreate()必须先调用onCreateView(),所以ListViews没有初始化,发现空指针异常。

于 2013-10-10T06:40:45.347 回答
0

只需将您的列表视图代码放在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();
    }
}
于 2014-04-03T14:53:42.777 回答
0

问题是当您在同一活动中调用任何其他布局文件时,您正在另一个布局文件中创建您的ListView 。因此它在调用活动的布局中不匹配。因此,将ListView放置在您的活动使用的相同布局中,或者如果您想要ListView在另一个布局然后更改ArrayAdapter对象上下文,例如: 将其更改为

ArrayAdapter 适配器 = new ArrayAdapter(this, R.layout., R.id., );

有了这个:

ArrayAdapter 适配器 = new ArrayAdapter(, R.layout., R.id., );

于 2014-02-03T11:22:47.353 回答