java.lang.RuntimeException:无法启动活动ComponentInfo {}:android.view.InflateException:二进制XML文件行#18:错误膨胀类

时间:2013-11-19 23:46:55

标签: android android-activity

我有问题。一切正常,但我决定改变我的应用程序的设计,似乎小部件按钮不起作用,我不知道为什么。 请帮帮我。


Logcat exection

11-19 23:30:31.631: E/AndroidRuntime(361): FATAL EXCEPTION: main
11-19 23:30:31.631: E/AndroidRuntime(361): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.activity/com.android.moyenne.note.AddNote}: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.os.Looper.loop(Looper.java:123)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-19 23:30:31.631: E/AndroidRuntime(361):  at java.lang.reflect.Method.invokeNative(Native Method)
11-19 23:30:31.631: E/AndroidRuntime(361):  at java.lang.reflect.Method.invoke(Method.java:521)
11-19 23:30:31.631: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-19 23:30:31.631: E/AndroidRuntime(361):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-19 23:30:31.631: E/AndroidRuntime(361):  at dalvik.system.NativeStart.main(Native Method)
11-19 23:30:31.631: E/AndroidRuntime(361): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.createView(LayoutInflater.java:513)
11-19 23:30:31.631: E/AndroidRuntime(361):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
11-19 23:30:31.631: E/AndroidRuntime(361):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.Activity.setContentView(Activity.java:1647)
11-19 23:30:31.631: E/AndroidRuntime(361):  at com.android.moyenne.note.AddNote.onCreate(AddNote.java:54)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-19 23:30:31.631: E/AndroidRuntime(361):  ... 11 more
11-19 23:30:31.631: E/AndroidRuntime(361): Caused by: java.lang.reflect.InvocationTargetException
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.widget.Button.<init>(Button.java:65)
11-19 23:30:31.631: E/AndroidRuntime(361):  at java.lang.reflect.Constructor.constructNative(Native Method)
11-19 23:30:31.631: E/AndroidRuntime(361):  at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.LayoutInflater.createView(LayoutInflater.java:500)
11-19 23:30:31.631: E/AndroidRuntime(361):  ... 23 more
11-19 23:30:31.631: E/AndroidRuntime(361): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.view.View.<init>(View.java:2036)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.widget.TextView.<init>(TextView.java:327)
11-19 23:30:31.631: E/AndroidRuntime(361):  at android.widget.Button.<init>(Button.java:69)

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fond_blue"
    tools:context="com.android.moyenne.note.AddNote" >

    <LinearLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@color/white"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_back_note"
            style="@style/Widget.Sherlock.ActionButton"
            android:layout_width="150dip"
            android:layout_height="30dip"
            android:layout_marginLeft="0dip"
            android:text="@string/button_back"
            android:textColor="@color/blue_color"
            android:textSize="20sp"
            android:textStyle="bold" />

        <View
            android:layout_width="2dip"
            android:layout_height="30dip"
            android:background="@drawable/fond_blue" />

        <Button
            android:id="@+id/btn_add_note"
            style="@style/Widget.Sherlock.ActionButton"
            android:layout_width="150dip"
            android:layout_height="30dip"
            android:text="@string/button_save"
            android:textColor="@color/blue_color"
            android:textSize="20sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/relativeLayout1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/type_note"
                    android:textColor="@color/white" />

                <Spinner
                    android:id="@+id/spnTypeNote"
                    style="@style/Widget.Sherlock.Spinner.DropDown.ActionBar"
                    android:layout_width="200dip"
                    android:layout_height="50dip"
                    android:ems="10"
                    android:text="@string/compo" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/note"
                    android:textColor="@color/white" />

                <EditText
                    android:id="@+id/txtNote"
                    android:layout_width="200dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edit_text_holo_light"
                    android:ems="10"
                    android:inputType="number|numberDecimal"
                    android:maxLength="5"
                    android:text="0.0" >

                    <requestFocus />
                </EditText>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/coef"
                    android:textColor="@color/white" />

                <EditText
                    android:id="@+id/txtCoef"
                    android:layout_width="200dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edit_text_holo_light"
                    android:ems="10"
                    android:inputType="number|numberDecimal"
                    android:maxLength="3" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/compo"
                    android:textColor="@color/white" />

                <EditText
                    android:id="@+id/date_compo"
                    android:layout_width="200dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edit_text_holo_light"
                    android:ems="10"
                    android:hint="@string/hint_date"
                    android:inputType="date" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/recu"
                    android:textColor="@color/white" />

                <EditText
                    android:id="@+id/date_recu"
                    android:layout_width="200dip"
                    android:layout_height="wrap_content"
                    android:background="@drawable/edit_text_holo_light"
                    android:ems="10"
                    android:hint="@string/hint_date"
                    android:inputType="date" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

我的活动课

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.Spannable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.android.activity.R;
import com.android.moyenne.activity.DatabaseHelper;

public class AddNote extends Activity implements OnItemSelectedListener {

    EditText txt_note;
    EditText txt_coef;
    Button btnAdd;
    protected int matId;
    protected int perId;
    protected int anId;
    protected String matMat;
    protected String perPer;
    protected String anAn;
    Button btnBack;
    Spinner spnTypeNote;
    String spnItem;
    EditText dateCompo;
    EditText dateRecu;
    DatabaseHelper db;

    private int year;
    private int month;
    private int day;
    static final int DATE_DIALOG_RECU_ID = 999;
    static final int DATE_DIALOG_COMPO_ID = 9991;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.note_ajouter);
        setTitle(R.string.ajouter_note);
        btnAdd = (Button) findViewById(R.id.btn_add_note);
        btnBack = (Button) findViewById(R.id.btn_back_note);
        txt_note = (EditText) findViewById(R.id.txtNote);
        txt_coef = (EditText) findViewById(R.id.txtCoef);
        dateCompo = (EditText) findViewById(R.id.date_compo);
        dateRecu = (EditText) findViewById(R.id.date_recu);
        spnTypeNote = (Spinner) findViewById(R.id.spnTypeNote);
        spnTypeNote.setOnItemSelectedListener(this);

        // ------------------ affichage Matiere periode et année scolaire
        // -------------------------
        // ------------------ recuperation des données venant de l'activity
        // précedente --------------------------
        matId = getIntent().getIntExtra("MATIERE_ID", 0);

        anId = getIntent().getIntExtra("ANSCO_ID", 0);

        perId = getIntent().getIntExtra("PERIODE_ID", 0);

        // -----------Gestion du spinner-----------------------------

        // Creating adapter for spinner
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                getApplicationContext(), R.array.type_not,
                android.R.layout.simple_spinner_item);

        // Drop down layout style - list view with radio button
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spnTypeNote.setAdapter(adapter);

        // --------------Gestion des dates -----------------
        setCurrentDateOnView(dateCompo);
        setCurrentDateOnView(dateRecu);

        btnAdd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    SaveNote();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(),
                            "  Erreur \n" + e.toString(), Toast.LENGTH_LONG)
                            .show();
                }
            }
        });

        btnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // --------------Gestion des dates -----------------

        dateCompo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_COMPO_ID);

            }
        });

        dateRecu.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_RECU_ID);

            }
        });

    }

    public void SaveNote() {
        boolean ok = true;
        db = new DatabaseHelper(getApplicationContext());
        Spannable spn = txt_coef.getText();
        Double coef = Double.valueOf(spn.toString());
        String datRecu = dateRecu.getText().toString();
        String datCompo = dateCompo.getText().toString();
        String spnNot = txt_note.getText().toString();
        Double note = getNumber(spnNot);
        if ((txt_coef.getText().toString().length() > 0)
                && (dateRecu.getText().toString().length() > 0)
                && (dateCompo.getText().toString().length() > 0)
                && (txt_note.getText().toString().length() > 0)
                && (spnItem.trim().length() > 0)) {
            try {
                db.insererNote(spnItem, note, coef, datRecu, datCompo, matId,
                        perId, anId);
            } catch (Exception ex) {
                ok = false;
                Toast.makeText(getApplicationContext(),
                        "  Erreur \n" + ex.toString(), Toast.LENGTH_LONG)
                        .show();

            } finally {
                if (ok) {
                    Toast.makeText(this, R.string.notif_enre, Toast.LENGTH_LONG)
                            .show();
                }
            }
            db.close();
            finish();
        }

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        // On selecting a spinner item
//      int idItem = parent.getId();
        Log.i("getid", position+"");
        if(position == 0){
            spnItem = "DEVOIR";
        }else if(position == 1){
            spnItem = "EVALUATION";
        }else if(position == 2){
            spnItem = "INTERROGATION";
        }
//      spnItem = parent.getItemAtPosition(position).toString();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_RECU_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerRecuListener, year,
                    month, day);

        case DATE_DIALOG_COMPO_ID:
            // set date picker as current date
            return new DatePickerDialog(this, datePickerCompoListener, year,
                    month, day);
        }
        return null;
    }

    public void setCurrentDateOnView(TextView tv) {

        final Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);

        // set current date into textview
        tv.setText(new StringBuilder()
                // Month is 0 based, just add 1
                .append(day).append("-").append(month + 1).append("-")
                .append(year));

    }

    private DatePickerDialog.OnDateSetListener datePickerCompoListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            dateCompo.setText(new StringBuilder().append(day).append("-")
                    .append(month + 1).append("-").append(year));

        }
    };

    private DatePickerDialog.OnDateSetListener datePickerRecuListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {
            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // set selected date into textview
            dateRecu.setText(new StringBuilder().append(day).append("-")
                    .append(month + 1).append("-").append(year));

        }
    };

    public double getNumber(String s) {
        double n = Double.parseDouble(s);
        return n;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;

    }
}

第18行是这个集团的开始

 #18         <Button
                    android:id="@+id/btn_back_note"
                    style="@style/Widget.Sherlock.ActionButton"
                    android:layout_width="150dip"
                    android:layout_height="30dip"
                    android:layout_marginLeft="0dip"
                    android:text="@string/button_back"
                    android:textColor="@color/blue_color"
                    android:textSize="20sp"
                    android:textStyle="bold" />

1 个答案:

答案 0 :(得分:0)

如果您没有编辑按钮的属性两次,一次是创建按钮,另一次是创建应答器样式,那么您是否检查了自己的样式?

例如:这里属性textColor被编辑了两次!

中的

        <Button
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageButton1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:background="@drawable/button_empty"
        android:text="Email"
        android:textColor="@Color/greycolor"
        android:textStyle="@style/buttonFont" />
你的style.xml中的

 <style name="buttonFont">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:typeface">normal</item>
    <item name="android:textSize">11dip</item>
</style>