致命异常:主要 - AsyncTask Json Parser

时间:2014-06-09 11:46:14

标签: android json eclipse android-asynctask nullpointerexception

使用JSON处理需要从数据库接收一些信息的应用程序,我无法弄清楚什么是错误的。它突然停止了工作。

logcat的:

06-09 07:41:18.654: E/AndroidRuntime(1926): FATAL EXCEPTION: main
06-09 07:41:18.654: E/AndroidRuntime(1926): android.view.InflateException: Binary XML file line #2: Error inflating class android.widget.RelativeLayout
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:121)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.ListView.onMeasure(ListView.java:1159)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.measure(View.java:15848)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.measure(View.java:15848)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.measure(View.java:15848)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.measure(View.java:15848)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.measure(View.java:15848)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.Choreographer.doFrame(Choreographer.java:532)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.os.Handler.handleCallback(Handler.java:730)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.os.Looper.loop(Looper.java:137)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.app.ActivityThread.main(ActivityThread.java:5103)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at java.lang.reflect.Method.invoke(Method.java:525)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at dalvik.system.NativeStart.main(Native Method)
06-09 07:41:18.654: E/AndroidRuntime(1926): Caused by: java.lang.reflect.InvocationTargetException
06-09 07:41:18.654: E/AndroidRuntime(1926):     at java.lang.reflect.Constructor.constructNative(Native Method)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
06-09 07:41:18.654: E/AndroidRuntime(1926):     ... 44 more
06-09 07:41:18.654: E/AndroidRuntime(1926): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f080000 a=-1 r=0x7f080000}
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.content.res.Resources.loadDrawable(Resources.java:2063)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.<init>(View.java:3364)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.View.<init>(View.java:3293)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.view.ViewGroup.<init>(ViewGroup.java:453)
06-09 07:41:18.654: E/AndroidRuntime(1926):     at android.widget.RelativeLayout.<init>(RelativeLayout.java:242)
06-09 07:41:18.654: E/AndroidRuntime(1926):     ... 47 more

无法找到要查找的行,为什么它应该停止工作?我附上了ListActivity(包含代码):

public class ListActivity extends Activity {

  ListView list;
  TextView icon;
  TextView name;
  TextView developer;
  TextView size;

  Button Btngetdata;

  ArrayList<HashMap<String, String>> mList = new ArrayList<HashMap<String, String>>();

  private static String url = "http://appwhittle.com/getdata.php";

  private static final String TAG_ITEM = "app_item";
  private static final String TAG_ICON = "app_icon";
  private static final String TAG_NAME = "app_name";
  private static final String TAG_DEVELOPER = "app_developer";
  private static final String TAG_SIZE = "app_size";

  JSONArray mJsonArray = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        mList = new ArrayList<HashMap<String, String>>();

        new JSONParse().execute();
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
       private ProgressDialog pDialog;
      @Override
        protected void onPreExecute() {
            super.onPreExecute();

            icon = (TextView)findViewById(R.id.icon);
            name = (TextView)findViewById(R.id.name);
            size = (TextView)findViewById(R.id.size);
            developer = (TextView)findViewById(R.id.developer);

            pDialog = new ProgressDialog(ListActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
      }

      @Override
        protected JSONObject doInBackground(String... args) {
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        return json;
      }
       @Override
         protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
            mJsonArray = json.getJSONArray(TAG_ITEM);
            for(int i = 0; i < mJsonArray.length(); i++){
            JSONObject c = mJsonArray.getJSONObject(i);

            String name = c.getString(TAG_NAME);
            String size = c.getString(TAG_SIZE);
            String developer = c.getString(TAG_DEVELOPER);
            String icon = c.getString(TAG_ICON);

            HashMap<String, String> map = new HashMap<String, String>();
            map.put(TAG_ICON, icon);
            map.put(TAG_NAME, name);
            map.put(TAG_DEVELOPER, "Developer: " + developer);
            map.put(TAG_SIZE, size + " MB");

            mList.add(map);
            list=(ListView)findViewById(R.id.list);

            ListAdapter adapter = new SimpleAdapter(ListActivity.this, mList,
                R.layout.list_v,
                new String[] {TAG_NAME, TAG_DEVELOPER, TAG_SIZE }, new int[] {
                    R.id.name, R.id.developer, R.id.size});

            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Toast.makeText(ListActivity.this, "You Clicked at "+ mList.get(+position).get(TAG_NAME), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        } catch (JSONException e) {
          e.printStackTrace();
        }
       }
    }        
}

JSONParser:

public class JSONParser {
  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";
  // constructor
  public JSONParser() {}

  public JSONObject getJSONFromUrl(String url) {
    // Making HTTP request
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);
      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
          is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
      is.close();
      json = sb.toString();
    } catch (Exception e) {
      Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    // try parse the string to a JSON object
    try {
      jObj = new JSONObject(json);
    } catch (JSONException e) {
      Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    // return JSON String
    return jObj;
  }
}

activity_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

非常感谢任何帮助!感谢

1 个答案:

答案 0 :(得分:0)

修正了它。看起来像我在我的布局中添加了android:background="@style/AppBaseTheme",它现在正在运行。感谢