在我的手机上运行我的Android应用程序时出现InflateException错误

时间:2016-02-12 11:04:52

标签: android runtime-error android-xml inflate-exception

希望有人能够帮助我解决这个问题。一直试图修复它几个小时 - 在logcat下面我相信重要的一行是Error inflating class。我在下面发布了我的代码。该程序包编译但在我运行时直接在我的手机上关闭。

感谢您的帮助。

我在drawable或OutOfMemoryError解决方案中看到了类似的问题,但似乎没有一个与我的问题相关。

02-12 10:38:55.390  31692-31692/com.example.chris.sunil_gupta D/dalvikvm﹕ Late-enabling CheckJNI
    02-12 10:38:55.400  31692-31698/com.example.chris.sunil_gupta E/jdwp﹕ Failed writing handshake bytes: Broken pipe (-1 of 14)
    02-12 10:38:55.400  31692-31698/com.example.chris.sunil_gupta D/dalvikvm﹕ Debugger has detached; object registry had 0 entries
    02-12 10:38:55.420  31692-31692/com.example.chris.sunil_gupta D/ActivityThread﹕ setTargetHeapUtilization:0.75
    02-12 10:38:55.420  31692-31692/com.example.chris.sunil_gupta D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
    02-12 10:38:55.450  31692-31692/com.example.chris.sunil_gupta D/AndroidRuntime﹕ Shutting down VM
    02-12 10:38:55.450  31692-31692/com.example.chris.sunil_gupta W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415998e0)
    02-12 10:38:55.450  31692-31692/com.example.chris.sunil_gupta E/AndroidRuntime﹕ FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chris.sunil_gupta/com.example.chris.sunil_gupta.MainActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
                at android.app.ActivityThread.access$900(ActivityThread.java:148)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chris.sunil_gupta" >

    <uses-permission android:name="android.permission.READ_CALL_LOG"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

package com.example.chris.sunil_gupta;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listview=null;
    private String callType=null;
    private String phoneNumber=null;
    private String callDate=null;
    private String callDuration=null;
    private Date callDateTime=null;

    private List <CallData>list = new ArrayList<CallData>();
    private Context context=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;

        listview=(ListView)findViewById(R.id.listView_calldata);

        getCallDetails();
        CustomAdapter adapter = new CustomAdapter(MainActivity.this, list);
        listview.setAdapter(adapter);
    }

    public void getCallDetails()
    {
        @SuppressWarnings("deprecation")
        Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);

        int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
        int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
        int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);


        while (managedCursor.moveToNext())
        {

            phoneNumber = managedCursor.getString(number);
            callType = managedCursor.getString(type);
            callDate = managedCursor.getString(date);

            callDateTime = new Date(Long.valueOf(callDate));

            callDuration = managedCursor.getString(duration);

            String cType = null;

            int cTypeCode = Integer.parseInt(callType);

            switch(cTypeCode)
            {
                case CallLog.Calls.OUTGOING_TYPE:
                    cType = "OUTGOING";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    cType= "INCOMING";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    cType = "MISSED";
                    break;
            }

            CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
            list.add(calldata);
        }

        managedCursor.close();
    }
}

CustomAdapter.java

package com.example.chris.sunil_gupta;

import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomAdapter extends ArrayAdapter<CallData>{

    private List <CallData> listdata=null;
    private LayoutInflater mInflater=null;
    public CustomAdapter(Activity context, List <CallData> calldata) {
        super(context, 0);
        this.listdata=calldata;
        mInflater = context.getLayoutInflater();
    }


    @Override
    public int getCount() {
        return listdata.size();
    }


    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null || convertView.getTag() == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.list_row, null);

            holder.callnumber = (TextView) convertView.findViewById(R.id.textView_callnumber);
            holder.calltype = (TextView) convertView.findViewById(R.id.textView_calltype);
            holder.calldate = (TextView) convertView.findViewById(R.id.textView_calldate);
            holder.callduration = (TextView) convertView.findViewById(R.id.textView_callduration);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        CallData calldatalist = listdata.get(position);
        String callnumber=calldatalist.getCallnumber();
        String calltype=calldatalist.getCalltype();
        Date calldate= calldatalist.getCalldatetime();
        String callduration=calldatalist.getCallduration();

        holder.callnumber.setText("Call Number: "+callnumber);
        holder.calltype.setText("Call Type: "+calltype);
        holder.calldate.setText("Call Date: "+String.valueOf(calldate));
        holder.callduration.setText("Duration: "+callduration);

        return convertView;
    }

    private static class ViewHolder {
        TextView callnumber;
        TextView calltype;
        TextView calldate;
        TextView callduration;
    }

}

CallData.java

package com.example.chris.sunil_gupta;

import java.io.Serializable;
import java.util.Date;

public class CallData implements Serializable{

    private String calltype;
    private String callnumber;
    private Date calldatetime;
    private String callduration;

    public CallData()
    {

    }

    public CallData(String calltype, String callnumber, Date calldatetime, String callduration)
    {
        this.calldatetime=calldatetime;
        this.callduration=callduration;
        this.callnumber=callnumber;
        this.calltype=calltype;
    }

    public String getCalltype() {
        return calltype;
    }

    public void setCalltype(String calltype) {
        this.calltype = calltype;
    }

    public String getCallnumber() {
        return callnumber;
    }

    public void setCallnumber(String callnumber) {
        this.callnumber = callnumber;
    }

    public Date getCalldatetime() {
        return calldatetime;
    }

    public void setCalldatetime(Date calldatetime) {
        this.calldatetime = calldatetime;
    }

    public String getCallduration() {
        return callduration;
    }

    public void setCallduration(String callduration) {
        this.callduration = callduration;
    }


}

activity_main.xml中

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    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=".MainActivity">

    <listview android:id="@+id/listView_calldata"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </listview>

</linearlayout>

list_row.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <linearlayout android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <textview android:id="@+id/textView_calltype"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="TextView">

            <textview android:id="@+id/textView_callnumber"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="TextView_callnumber">

                <textview android:id="@+id/textView_calldate"
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="TextView">

                    <textview android:id="@+id/textView_callduration"
                        android:layout_height="wrap_content"
                        android:layout_width="wrap_content"
                        android:text="TextView">

                    </textview>
                </textview>
            </textview>
        </textview>
    </linearlayout>

</linearlayout>

3 个答案:

答案 0 :(得分:1)

您正在使用

<linearlayout> instead of <LinearLayout>

只需更换它,然后再试一次!希望你能获得成功!

答案 1 :(得分:1)

您在activity_main.xmllist_row.xml文件中使用错误的xmls标记,您需要更改它们:

linearlayout - &gt; LinearLayout

textview - &gt; TextView

listview - &gt; ListView

答案 2 :(得分:1)

更改

list_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/textView_calltype"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Textview
    android:id="@+id/textView_callnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView_callnumber" />

<TextView
    android:id="@+id/textView_calldate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView_callduration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />


</LinearLayout>

<强> activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
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=".MainActivity">

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

</LinearLayout>

同样更正“活动”中的标记名称。