在我的应用程序中错误膨胀类碎片

时间:2015-02-12 13:19:15

标签: android json eclipse

我的logcat是

 java.lang.RuntimeException:        Unable to start activity        ComponentInfo{com.example.googlemapandroidv2/com.example.googlemapandroidv2.Promotions}: android.view.InflateException: Binary XML file line #30: Error inflating class fragment
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.os.Looper.loop(Looper.java:150)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.ActivityThread.main(ActivityThread.java:4277)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at java.lang.reflect.Method.invoke(Method.java:507)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at dalvik.system.NativeStart.main(Native Method)
02-12 18:08:51.707: E/AndroidRuntime(13169): Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class fragment
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.Activity.setContentView(Activity.java:1712)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at com.example.googlemapandroidv2.Promotions.onCreate(Promotions.java:41)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
02-12 18:08:51.707: E/AndroidRuntime(13169):    ... 11 more
02-12 18:08:51.707: E/AndroidRuntime(13169): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.googlemapandroidv2-1.apk]
02-12 18:08:51.707: E/AndroidRuntime(13169):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
02-12 18:08:51.707: E/AndroidRuntime(13169):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-12 18:08:51.707: E/AndroidRuntime(13169):    ... 21 more*
启动此活动时

。我在HTC wildfireS 2.3安卓版

上尝试这个应用程序
package com.example.googlemapandroidv2;

import java.util.ArrayList;
import com.example.googlemapandroidv2.R;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Promotions extends ListActivity {

private Context context;
private static String url = "http://expressdiner.ifeelhungry.co.uk/pushadmin/webservice/getPermotions.php";

private static final String ID = "idj";
private static final String TITLE = "titlej";
private static final String URL = "urlj";
private static final String OFFER = "offerj";
private static final String ENDDATE = "enddatej";

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

ListView lv ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new ProgressTask(Promotions.this).execute();
}

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;

    private ListActivity activity;

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }

    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                R.layout.list_item, new String[] { ID, TITLE,
                        URL, OFFER, ENDDATE }, new int[] {
                        R.id.id, R.id.title, R.id.url,
                        R.id.offer, R.id.enddate });

        setListAdapter(adapter);
        Log.e("INside of","Promotions");
        // select single ListView item
         lv = getListView();
    }

    protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();

        // get JSON data from URL
        JSONArray json = jParser.getJSONFromUrl(url);

        for (int i = 0; i < json.length(); i++) {

            try {
                JSONObject c = json.getJSONObject(i);
                String idj = c.getString(ID);

                String titlej = c.getString(TITLE);
                String urlj = c.getString(URL);
                String offerj = c.getString(OFFER);
                String enddatej =c.getString(ENDDATE);
                HashMap<String, String> map = new HashMap<String, String>();

                // Add child node to HashMap key & value
                map.put(ID, idj);
                map.put(TITLE, titlej);
                map.put(URL, urlj);
                map.put(OFFER, offerj);
                map.put(ENDDATE, enddatej);
                jsonlist.add(map);
                Log.e(offerj, "Offer");
                Log.e(titlej, "title is");
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
  }
}

活动的XML是我试图列出通过wevservice解析的数据

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.googlemapandroidv2.Promotions" >

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


    </LinearLayout>

2 个答案:

答案 0 :(得分:1)

布局xml文件不正确(在xml文件的第30行)。

有一个xml标记<android.view.fragment>,它不是有效标记。它应该只是<fragment>而已。

<强>更新

如果你膨胀

<fragment android:id="@+id/map"
  android:name="com.google.android.gms.maps.SupportMapFragment"
  android:layout_width="match_parent" android:layout_height="match_parent" />

在某处,您必须确保LayoutInflater使用的FragmentActivity是从FragmentActivity(支持库)实例“创建”的。只有<fragment>知道如何在Android 3之前处理SupportMapFragment标记。我不知道您在Promotions extends ListActivity的使用位置,但如果您在SupportMapFragement中使用它,则无效。

例如,您在适配器中使用的R.layout.list_item中使用FragmentActivity吗?这就是问题所在,因为Fragment仅适用于从ActionBarActivity延伸的活动(如ListViewActivity)。 FragmentActivity并未延伸至{{1}}。

答案 1 :(得分:-1)

我也遇到了这个问题,我只是清理项目并再次运行它。多数民众赞成。