ArrayeAdapter正在获取空上下文

时间:2014-01-22 14:33:10

标签: android json listview android-custom-view

NullPointerException android.widget.ArrayAdapter.init(ArrayAdapter.java)只是将Arraylist值添加到customAdapter类中并将这些值设置为listview,请参阅下面的代码helpm me

我的logcat错误:

01-22 14:50:46.049: E/i(26881): بعد بهش میگن با نجیب جمله بساز :میگه یدونه کت دارم نه جیب داره نه دکمه...!!
01-22 14:50:46.049: E/i(26881): به یک نفر میگن با جام جم جمله بساز میگه :مامان بیا جامو جم کن...
01-22 14:50:46.049: W/dalvikvm(26881): threadid=1: thread exiting with uncaught exception (group=0x417e9a08)
01-22 14:50:46.059: E/AndroidRuntime(26881): FATAL EXCEPTION: main
01-22 14:50:46.059: E/AndroidRuntime(26881): java.lang.NullPointerException
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.widget.ArrayAdapter.init(ArrayAdapter.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at it.gmariotti.cardslib.library.internal.CardArrayAdapter.<init>(CardArrayAdapter.java:116)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at ir.reza.smss.Fragments.TabFragment1.populateSpinner(TabFragment1.java:75)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at ir.reza.smss.GetCategories.onPostExecute(GetCategories.java:88)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at ir.reza.smss.GetCategories.onPostExecute(GetCategories.java:1)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.os.AsyncTask.finish(AsyncTask.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.os.AsyncTask.access$600(AsyncTask.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.os.Handler.dispatchMessage(Handler.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.os.Looper.loop(Looper.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at android.app.ActivityThread.main(ActivityThread.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at java.lang.reflect.Method.invokeNative(Native Method)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at java.lang.reflect.Method.invoke(Method.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
01-22 14:50:46.059: E/AndroidRuntime(26881):    at dalvik.system.NativeStart.main(Native Method)

这是TabFragment1:

package ir.reza.smss.Fragments;

import ir.reza.smss.Category;
import ir.reza.smss.GetCategories;
import ir.reza.smss.GooglePlaySmallCard;
import ir.reza.smss.R;
import ir.reza.smss.ServiceHandler;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.view.CardListView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;


@SuppressLint("ValidFragment")
public class TabFragment1 extends SherlockFragment  {
    // array list for spinner adapter
    ProgressDialog pDialog;
    // Url to get all categories
    private String URL_CATEGORIES = "http://varzesh-ilam.xzn.ir/app/get_categories.php";

    public static ArrayList<Category> categoriesList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tab_1, container, false);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);



        GetCategories webServiceTask = new GetCategories(this.getActivity());
        webServiceTask.execute();



    }

    public void populateSpinner() {


        ArrayList<Card> cards = new ArrayList<Card>();

        for (int i = categoriesList.size()-1; i >= 0; --i) {

            GooglePlaySmallCard card = new GooglePlaySmallCard(this.getActivity());
            card.setTitle(categoriesList.get(i).getName());
            card.count=i;
            Log.e("i", categoriesList.get(i).getName());

            cards.add(card);
        }
        CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(),cards);

        CardListView listView = (CardListView) getActivity().findViewById(R.id.carddemo_list_gplaycard);
        if (listView!=null){
            listView.setAdapter(mCardArrayAdapter);
        }

    }




}

这是GetCategories:

package ir.reza.smss;


import ir.reza.smss.Fragments.TabFragment1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class GetCategories extends AsyncTask<Void, Void, Void> {

    private Context context;

    ProgressDialog pDialog;
    // Url to get all categories
    private String URL_CATEGORIES = "http://varzesh-ilam.xzn.ir/app/get_categories.php";

    public GetCategories(Context context) {
        this.context = context;
        TabFragment1.categoriesList = new ArrayList<Category>();
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Loading...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        ServiceHandler jsonParser = new ServiceHandler();
        String json = jsonParser.makeServiceCall(URL_CATEGORIES, ServiceHandler.GET);

        Log.e("Response: ", "> " + json);

        if (json != null) {
            try {
                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray categories = jsonObj
                            .getJSONArray("categories");                        

                    for (int i = 0; i < categories.length(); i++) {
                        JSONObject catObj = (JSONObject) categories.get(i);
                        Category cat = new Category(catObj.getInt("id"),
                                catObj.getString("name"));
                        TabFragment1.categoriesList.add(cat);
                        Log.e("categories", "sakhte shod loop ham por shod");
                    }
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        TabFragment1 tb1 = new TabFragment1();
        tb1.populateSpinner();
    }



}

我在Android开发新手请帮我代码请帮忙.......

0 个答案:

没有答案