2

私はこれで丸一日頭を悩ませていたので、助けを求めることにしました。このフラグメントを取得して listView を適切に設定しようとしていますが、何をしても nullpointer 例外が発生します。本当に小さなものが欠けていると確信しています。誰かがそれを指摘してくれることを願っています:)

package com.jaymzzz;

import android.app.Activity;
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 android.widget.ListView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockListFragment;
import com.actionbarsherlock.widget.SearchView;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class TestAPICallFragment extends SherlockListFragment implements SearchView.OnQueryTextListener {
ListView listView;
ItemListAdapter itemListAdapter;
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);

}

@Override
public void onResume() {
    super.onResume();
    new JSONFromAPI().execute(new String[]{"https://api.jaymzzz.com/api/json"});
}

public void onAttach(Activity activity) {
    super.onAttach(activity);
    context = this.getActivity();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View fragmentView = inflater.inflate(R.layout.fragment, container, false);
    listView = (ListView) fragmentView.findViewById(android.R.id.list);
    return fragmentView;
}


@Override
public boolean onQueryTextSubmit(String query) {
    Toast.makeText(getActivity(), query, Toast.LENGTH_SHORT);
    Log.d("errlog", "Search query = " + query);
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    Log.d("errlog", "Search query changed to " + newText);
    return false;  //To change body of implemented methods use File | Settings | File Templates.
}

private class JSONFromAPI extends AsyncTask<String, Void, JSONObject> {

    ProgressDialog dialog = new ProgressDialog(getActivity());

    protected void onPreExecute() {
        dialog.setMessage("Loading...");
        dialog.show();
    }

    protected JSONObject doInBackground(String... urlString) {
        String url = urlString[0];

        JSONObject json = new utilGetJSONFromAPI().getJSONFromAPI(url);
        Log.d("errlog", "Leaving doInBackground");

        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        return json;
    }

    protected void onPostExecute(JSONObject json) {

        Log.d("errlog", "Initiating onPostExecute()");

        List<ItemEntry> items = new ArrayList<ItemEntry>();

        try {
            JSONArray message = json.getJSONArray("message");

            Log.d("errlog", "Message size: " + message.length());


            for (int i = 0; i < message.length(); i++) {
                JSONObject e = message.getJSONObject(i);
                items.add(new ItemEntry(e.getString("item_name"), e.getString("item_brand_name"), e.getInt("item_id")));

            }
        } catch (Exception e) {
            Log.e("errlog", "Error parsing data " + e.toString());
        }

        itemListAdapter = new ItemListAdapter(context, R.layout.list_item, R.id.title, R.id.subtitle);

        try {
            listView.setAdapter(itemListAdapter); // THE LINE THAT KEEPS FAILING

            for (final ItemEntry entry : items) {
                itemListAdapter.add(entry);
            }
        } catch (Exception e) {
            Toast.makeText(context, "Error Loading data..", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }

    }
  }


}

ここにもスタックトレースがあります:

12-01 06:33:27.142: WARN/System.err(3003): java.lang.NullPointerException 12-01 06:33:27.142: WARN/System.err(3003): com.jaymzzz.TestAPICallFragment$JSONFromAPI で。 onPostExecute(TestAPICallFragment.java:117) 12-01 06:33:27.152: WARN/System.err(3003): com.jaymzzz.TestAPICallFragment$JSONFromAPI.onPostExecute(TestAPICallFragment.java:72) 12-01 06:33: 27.152: WARN/System.err(3003): >android.os.AsyncTask.finish(AsyncTask.java:602) 12-01 06:33:27.152: WARN/System.err(3003): android.os で。 AsyncTask.access$600(AsyncTask.java:156) 12-01 06:33:27.152: WARN/System.err(3003): android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 12-01 06 :33:27.152: WARN/System.err(3003): >android.os.Handler.dispatchMessage(Handler.java:99) 12-01 06:33:27.152: WARN/System.err(3003): Android で.os.Looper.loop(Looper.java:137) 12-01 06:33:27.152: WARN/System.err(3003): android.app.ActivityThread.main(ActivityThread.java:4340) で 12-01 06:33:27.152: WARN/System.err( 3003): java.lang.reflect.Method.invokeNative(Native >Method) 12-01 06:33:27.162: WARN/System.err(3003): at >java.lang.reflect.Method.invoke(Method. java:511) 12-01 06:33:27.162: WARN/System.err(3003): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-01 06:33: 27.162: WARN/System.err(3003): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 12-01 06:33:27.162: WARN/System.err(3003): dalvik で.system.NativeStart.main(ネイティブメソッド)メソッド) 12-01 06:33:27.162: WARN/System.err(3003): >java.lang.reflect.Method.invoke(Method.java:511) 12-01 06:33:27.162: WARN/システム.err(3003): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-01 06:33:27.162: WARN/System.err(3003): com.android で。 internal.os.ZygoteInit.main (ZygoteInit.java:551) 12-01 06:33:27.162: 警告/System.err (3003): dalvik.system.NativeStart.main (ネイティブ メソッド) でメソッド) 12-01 06:33:27.162: WARN/System.err(3003): >java.lang.reflect.Method.invoke(Method.java:511) 12-01 06:33:27.162: WARN/システム.err(3003): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 12-01 06:33:27.162: WARN/System.err(3003): com.android で。 internal.os.ZygoteInit.main (ZygoteInit.java:551) 12-01 06:33:27.162: 警告/System.err (3003): dalvik.system.NativeStart.main (ネイティブ メソッド) でmain(ネイティブメソッド)main(ネイティブメソッド)

助けてください :)

4

0 に答える 0