android.view.InflateException:二进制XML文件行#11:错误膨胀类片段并使应用程序崩溃

时间:2014-08-13 07:41:34

标签: android android-fragments

我是Android新手并尝试制作简单的App。我写了一个测试程序,用于连接互联网并在我的应用程序中显示数据,但是当它想要连接时,该对象为空,程序崩溃,我不知道它为什么会发生?

    package com.earthquake1;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.app.ListFragment;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ArrayAdapter;

public class EarthquakeListFragment extends ListFragment {

    ArrayAdapter<Quake> aa ;
    ArrayList<Quake> earthquakes = new ArrayList<Quake>();

    private static final String TAG = "EARTHQUAKE";
    private Handler handler = new Handler();

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

        int layoutID = android.R.layout.simple_list_item_1;
        aa = new ArrayAdapter<Quake>(this.getActivity(), layoutID ,earthquakes);
        setListAdapter(aa);

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                refreshEarthquakes();
            }
        });
        t.start();
    }

    protected void refreshEarthquakes() {


        URL url;
        try{
            //String quakeFeed = getString(R.string.quake_feed);
            url = new URL(getString(R.string.quake_feed));

            URLConnection connection ;
            connection = url.openConnection();



            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setAllowUserInteraction(false);
            httpConnection.setInstanceFollowRedirects(true);
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();
            int responseCode = httpConnection.getResponseCode();




            if(responseCode == HttpURLConnection.HTTP_OK){
                InputStream in = httpConnection.getInputStream();

                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document dom = db.parse(in);
                Element docEle = dom.getDocumentElement();

                earthquakes.clear();

                NodeList nl = docEle.getElementsByTagName("entry");
                if(nl != null && nl.getLength() >0 ){
                    for(int i = 0 ; i<nl.getLength(); i++){
                        Element entry = (Element) nl.item(i);
                        Element title = (Element) entry.getElementsByTagName("title").item(0);
                        Element g = (Element) entry.getElementsByTagName("georss:point").item(0);
                        Element when = (Element) entry.getElementsByTagName("updated").item(0);
                        Element link = (Element) entry.getElementsByTagName("link").item(0);

                        String details = title.getFirstChild().getNodeValue();
                        String hostName = "http://earthquake.usgs.gov";
                        String linkString = hostName + link.getAttribute("href");

                        String point;
                        if(g != null)
                            point = g.getFirstChild().getNodeValue();
                        else
                            point = "";
                        String dt = when.getFirstChild().getNodeValue();
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
                        Date qdate = new GregorianCalendar(0,0,0).getTime();
                        try{
                            qdate = sdf.parse(dt);
                        }catch (ParseException e){
                            Log.d(TAG,"Date parsing Exception.",e);
                        }

                        String[] location = point.split(" ");
                        Location l = new Location("dummyGPS");
                        if(g != null){
                            l.setLatitude(Double.parseDouble(location[0]));
                            l.setLongitude(Double.parseDouble(location[1]));
                        }
                        else{
                            l.setLatitude(0);
                            l.setLongitude(0);
                        }

                        String magnitudeString = details.split(" ")[1];
                        int end = magnitudeString.length()-1;
                        double magnitude = 0;
                        if(!magnitudeString.equals("Feed")){
                            magnitude = Double.parseDouble(magnitudeString.substring(0,end));
                            details = details.split(",")[1].trim();
                        }

                        final Quake quake = new Quake(qdate,details,l,magnitude,linkString);

                        handler.post(new Runnable() {

                            @Override
                            public void run() {
                                addNewQuake(quake);
                            }
                        });
                    }
                }

            }
        }
        catch (MalformedURLException e){
            Log.d(TAG,"MalformedURLException");
        } 
        catch (IOException e) {
            Log.d(TAG,"IOException");
        } 
        catch (ParserConfigurationException e) {
            Log.d(TAG,"ParserConfigurationException");
        } 
        catch (SAXException e) {
            Log.d(TAG,"SAXException");
        } 
        catch(Exception e){
            Log.d(TAG,e.getMessage());
        }
        finally {

        }
    }

    protected void addNewQuake(Quake quake) {
        earthquakes.add(quake);
        aa.notifyDataSetChanged();
    }
}

错误日志:

08-13 11:26:45.064: D/AndroidRuntime(31064): procName from cmdline: com.earthquake1
08-13 11:26:45.064: E/AndroidRuntime(31064): in writeCrashedAppName, pkgName :com.earthquake1
08-13 11:26:45.064: D/AndroidRuntime(31064): file written successfully with content: com.earthquake1 StringBuffer : ;com.earthquake1
08-13 11:26:45.064: E/AndroidRuntime(31064): FATAL EXCEPTION: main
08-13 11:26:45.064: E/AndroidRuntime(31064): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.earthquake1/com.earthquake1.Main}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.os.Looper.loop(Looper.java:137)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.ActivityThread.main(ActivityThread.java:4424)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at java.lang.reflect.Method.invokeNative(Native Method)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at java.lang.reflect.Method.invoke(Method.java:511)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at dalvik.system.NativeStart.main(Native Method)
08-13 11:26:45.064: E/AndroidRuntime(31064): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Activity.setContentView(Activity.java:1835)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at com.earthquake1.Main.onCreate(Main.java:12)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Activity.performCreate(Activity.java:4465)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-13 11:26:45.064: E/AndroidRuntime(31064):    ... 11 more
08-13 11:26:45.064: E/AndroidRuntime(31064): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.earthquake.EarthquakeListFragment: make sure class name exists, is public, and has an empty constructor that is public
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Fragment.instantiate(Fragment.java:581)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Fragment.instantiate(Fragment.java:549)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Activity.onCreateView(Activity.java:4235)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
08-13 11:26:45.064: E/AndroidRuntime(31064):    ... 21 more
08-13 11:26:45.064: E/AndroidRuntime(31064): Caused by: java.lang.ClassNotFoundException: com.earthquake.EarthquakeListFragment
08-13 11:26:45.064: E/AndroidRuntime(31064):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-13 11:26:45.064: E/AndroidRuntime(31064):    at android.app.Fragment.instantiate(Fragment.java:571)
08-13 11:26:45.064: E/AndroidRuntime(31064):    ... 24 more
08-13 11:26:47.154: D/AndroidRuntime(31095): Shutting down VM
08-13 11:26:47.154: W/dalvikvm(31095): threadid=1: thread exiting with uncaught exception (group=0xb2c2d180)
08-13 11:26:47.164: D/AndroidRuntime(31095): procName from cmdline: com.earthquake1
08-13 11:26:47.164: E/AndroidRuntime(31095): in writeCrashedAppName, pkgName :com.earthquake1

2 个答案:

答案 0 :(得分:2)

根本原因在于:

Caused by: java.lang.ClassNotFoundException: com.earthquake.EarthquakeListFragment

您的包名称似乎是com.earthquake1而不是com.earthquake。确保您的布局XML和代码在类和包名称上达成一致。

答案 1 :(得分:0)

无法实例化片段com.earthquake.EarthquakeListFragment:确保类名存在,是公共的,并且具有公共的空构造函数

制作EarthquakeListFragment的默认构造函数:

public EarthquakeListFragment(){
super();
}