ListView中setAdapter中的NullPointer异常

时间:2013-10-10 06:24:03

标签: android nullpointerexception simplecursoradapter

当wifi打开时 - 一切正常,但当它关闭时 - 我得到nullPointerexception

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

5 个答案:

答案 0 :(得分:5)

问题是您要在onCreateView()中创建ListView并在adapter中设置onCreate() ..

您应该知道onCreate()始终在onCreateView()之前被调用,到那时您的listview未被创建...这就是您获得NullPointerException的原因。

onCreate()的代码放在onCreateView()

答案 1 :(得分:0)

您的问题是,您已在ListView方法上创建了onCreateView(),并且已在onCreate()中设置了适配器。因此onCreate()必须在onCreateView()之前调用,这就是ListViews未初始化的原因,并且您找到了空指针异常。

答案 2 :(得分:0)

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

ArrayAdapter adapter = new ArrayAdapter(this,                 R.layout。,R.id。,);

ArrayAdapter adapter = new ArrayAdapter(,                 R.layout。,R.id。,);

答案 3 :(得分: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();
    }
}

答案 4 :(得分:0)

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