1

我正在使用片段创建 UI。在 onCreate(Bundle savedInstanceState) 中,我正在解析 JSON 数据。我收到以下错误:

07-17 15:23:18.089: W/System.err(1368): 在 android.os.Looper.loop(Looper.java:132) 07-17 15:23:18.129: E/AndroidRuntime(1368): 致命例外:主要 07-17 15:23:18.129:E/AndroidRuntime(1368):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.afghanread.android/com.afghanread.android.AfghanMagActivity}:android.view。 InflateException: Binary XML file line #8: Error inflating class fragment 07-17 15:23:18.129: E/AndroidRuntime(1368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748) 07-17 15:23 :18.129: E/AndroidRuntime(1368): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app.ActivityThread.access$1500 (ActivityThread.java:122) 07-17 15:23:18.129: E/AndroidRuntime(1368):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07- 17 15:23:18.129: E/AndroidRuntime(1368): 在 android.os.Looper.loop(Looper.java:132) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app。 ActivityThread.main(ActivityThread.java:4025) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-17 15:23:18.129: E /AndroidRuntime(1368): at java.lang.reflect.Method.invoke(Method.java:491) 07-17 15:23:18.129: E/AndroidRuntime(1368): at com.android.internal.os.ZygoteInit$ MethodAndArgsCaller.run(ZygoteInit.java:841) 07-17 15:23:18.129: E/AndroidRuntime(1368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 07-17 15:23:18.129:E/AndroidRuntime(1368):在 dalvik.system.NativeStart.main(本机方法)07-17 15:23:18.129:E/AndroidRuntime(1368):引起:android.view.InflateException:二进制 XML文件第 8 行:膨胀类片段时出错 07-17 15:23:18.129: E/AndroidRuntime(1368): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688) 07-17 15:23:18.129: E /AndroidRuntime(1368): 在 android.view.LayoutInflater.rInflate(LayoutInflater.java:724) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.view.LayoutInflater.inflate(LayoutInflater.java: 479) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:391) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在android.view.LayoutInflater.inflate(LayoutInflater.java:347) 07-17 15:23:18.129:E/AndroidRuntime(1368): 在 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app.Activity .setContentView(Activity.java:1780) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 com.afghanread.android.AfghanMagActivity.onCreate(AfghanMagActivity.java:26) 07-17 15:23:18.129 : E/AndroidRuntime(1368): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread. java:1712) 07-17 15:23:18.129: E/AndroidRuntime(1368): ... 11 更多 07-17 15:23:18.129: E/AndroidRuntime(1368): 引起:java.lang.NullPointerException 07 -17 15:23:18.129:E/AndroidRuntime(1368):在 org.json.JSONTokener。nextCleanInternal(JSONTokener.java:112) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 org.json.JSONTokener.nextValue(JSONTokener.java:90) 07-17 15:23:18.129: E/ AndroidRuntime(1368): 在 org.json.JSONObject.(JSONObject.java:154) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 org.json.JSONObject.(JSONObject.java:171) 07 -17 15:23:18.129: E/AndroidRuntime(1368): 在 com.afghanread.android.CategoryFragment.parseJSON(CategoryFragment.java:72) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 com .afghanread.android.CategoryFragment.onCreateView(CategoryFragment.java:161) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:713) 07-17 15 :23:18.129: E/AndroidRuntime(1368): 在 android.app.FragmentManagerImpl.moveToState(FragmentManager.java:905) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app.FragmentManagerImpl.addFragment(FragmentManager.java:983) 07-17 15:23:18.129: E/AndroidRuntime(1368): 在 android.app.Activity.onCreateView (活动.java:4141)

我的代码是:

public class CategoryFragment extends Fragment implements OnItemClickListener {

ArrayList<String> cat = new ArrayList<String>();
ListView list;
DatabaseManager manager;
static String DATABASE_NAME = "afghanread.sqlite";
static Integer DATABASE_VERSION = 1;
TextView text;
TextView cat_name;
int length;
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", "Linux",
        "OS/2" };

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    parseJSON();
    manager = new DatabaseManager(null, DATABASE_NAME, null,
            DATABASE_VERSION);

}

private void parseJSON() {
    try {
        JSONObject jObject = new JSONObject(getJSONData());
        JSONArray jArray = jObject.getJSONArray("data");
        length = jArray.length();
        for (int i = 0; i < length; i++) {
            JSONObject tmpJson = jArray.getJSONObject(i);
            Category category = new Category();

            category.setCategotyId(tmpJson.getInt("category_id"));
            category.setCategoryName(tmpJson.getString("category_name"));

            // manager.insertCategoryDetails(category);

        }
        System.out.println("Jason parsing successful.......");
    } catch (JSONException e) {

        e.printStackTrace();
    }
}

private String getJSONData() {
    HttpPost httpPost = null;
    HttpClient httpClient = null;
    HttpResponse httpResponse = null;
    HttpEntity entity = null;
    InputStream in = null;
    BufferedReader reader = null;
    StringBuilder sb = null;
    String line = null;
    String result = null;
    try {
        httpPost = new HttpPost(
                "http://183.182.84.197:88/Sayyed_fahim/mobile/category.php");
        httpClient = new DefaultHttpClient();
        httpResponse = httpClient.execute(httpPost);
        int statusCode = httpResponse.getStatusLine().getStatusCode();
        if (statusCode == 200) {
            entity = httpResponse.getEntity();
            in = entity.getContent();
            reader = new BufferedReader(new InputStreamReader(in));
            sb = new StringBuilder();
            while ((line = reader.readLine()) != null)
                sb.append(line);

            result = sb.toString();
            result = result.trim();
            result = result.substring(1, result.length());
            Log.i("RESULT", result);
        } else
            Log.e("Error", "" + statusCode);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
                in = null;
            }

            httpPost = null;
            httpClient = null;
            httpResponse = null;
            entity = null;
            in = null;
            reader = null;
            sb = null;
            line = null;

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

    return result;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    System.out.println("Inside the afghan Read category");
    View view = (View) inflater.inflate(R.layout.categoryfragment, null);
    list = (ListView) view.findViewById(R.id.cat_list);

    /*
     * ArrayAdapter<String> adapter = new
     * ArrayAdapter<String>(getActivity(),
     * android.R.layout.simple_list_item_1, values);
     */
    parseJSON();
    list.setAdapter(new CustomAdapter(getActivity(), R.layout.categoryitem,
            values));

    list.setOnItemClickListener(this);
    return view;
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    String item = (String) values[arg2];
    LibraryFragment fragment = (LibraryFragment) getFragmentManager()
            .findFragmentById(R.id.library);
    if (fragment != null && fragment.isInLayout()) {
        fragment.setText(item);
    } else {
        Intent intent = new Intent(getActivity().getApplicationContext(),
                LibraryActivity.class);
        intent.putExtra("value", item);
        startActivity(intent);
    }

}

public class CustomAdapter extends BaseAdapter {
    String names[];
    Context mCtx;
    int item;
    LayoutInflater inflater;

    public CustomAdapter(Context ctx, int categoryitem, String[] values) {

        this.names = values;
        this.mCtx = ctx;
        this.item = categoryitem;
        inflater = (LayoutInflater) ctx
                .getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return names.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = inflater.inflate(item, null);
            cat_name = (TextView) convertView.findViewById(R.id.cat_name);

        }

        cat_name.setText(names[position]);

        return convertView;

    }

}

}

如何解决问题??

4

1 回答 1

0

我猜网络调用失败,你将 null 传递给new JSONObject(getJSONData()).
是 JSONObject jObject = new JSONObject(getJSONData()); 第 72 行,作为 logcat 输出中空指针的来源:com.afghanread.android.CategoryFragment.parseJSON(CategoryFragment.java:72)?

与错误不完全相关,但也是代码中的一个大问题。您正在 UI 线程内进行网络通话。这将冻结手机,直到从您的服务器加载 json 数据。您至少应该将所有数据加载代码放在AsyncTask中,并在 onPostExecute 方法中更新您的 UI。

于 2012-07-17T10:41:49.790 回答