java - 二进制 XML 文件行 #12 : Error inflating class com. android.phone91.DialPadView

标签 java android xml performance

我在 setContentView(R.layout.home); 这条线上遇到错误。因为我第一次遇到这个问题,不知道如何解决它。看来它正在设计考虑问题但不知道那是什么 这是我的代码

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import com.actionbarsherlock.app.SherlockActivity;

    public class DialerActivity extends SherlockActivity{

    TextView number;
    Button call;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            Log.e("Dialer activity", "Dilaer Activity");
            **setContentView(R.layout.home);**
            number = (TextView)findViewById(R.id.displayNumber);
            call = ((Button)findViewById(R.id.buttonDial));
        }   
    }

这里是 xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/white"
        android:orientation="vertical" >

        <com.android.phone91.DialPadView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="#ffffff" />

        <Button
            android:id="@+id/buttonDial"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="40dp"
            android:layout_marginBottom="20dp"
            android:text="Call"
            android:textColor="@android:color/white"
            android:background="@drawable/button_background"
            android:gravity="center" >
        </Button>
    </LinearLayout>

</ScrollView>

拨号盘 View :

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DialPadView extends LinearLayout implements OnClickListener {
    LinearLayout buttons[] = new LinearLayout[12];
    Button clearNumber;
    TextView display;
    Vibrator vibe;
    int text_length;
    MediaPlayer mp;
    MainActivity hactivity;

    public DialPadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        hactivity = (MainActivity) context;
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li;
        li = (LayoutInflater) hactivity.getBaseContext().getSystemService(infService);
        li.inflate(R.layout.dialpad, this, true);
        vibe = (Vibrator) hactivity.getBaseContext().getSystemService(Context.VIBRATOR_SERVICE);

        this.display = (TextView) findViewById(R.id.displayNumber);

        this.buttons[0] = (LinearLayout) findViewById(R.id.button0);
        this.buttons[1] = (LinearLayout) findViewById(R.id.button1);
        this.buttons[2] = (LinearLayout) findViewById(R.id.button2);
        this.buttons[3] = (LinearLayout) findViewById(R.id.button3);
        this.buttons[4] = (LinearLayout) findViewById(R.id.button4);
        this.buttons[5] = (LinearLayout) findViewById(R.id.button5);
        this.buttons[6] = (LinearLayout) findViewById(R.id.button6);
        this.buttons[7] = (LinearLayout) findViewById(R.id.button7);
        this.buttons[8] = (LinearLayout) findViewById(R.id.button8);
        this.buttons[9] = (LinearLayout) findViewById(R.id.button9);
        this.buttons[10] = (LinearLayout) findViewById(R.id.buttonPlus);
        this.buttons[11] = (LinearLayout) findViewById(R.id.buttonPound);

        for (LinearLayout button: this.buttons) {
            button.setOnClickListener(this);
        }

        this.clearNumber = (Button) findViewById(R.id.buttonClearNumber);

        this.clearNumber.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                text_length = display.length();
                if (text_length > 0) {
                    //trim the last character off
                    String num = display.getText().toString();
                    display.setText(num.substring(0, num.length() - 1));
                }

                if (!((text_length - 1) > 0))
                    clearNumber.setVisibility(View.GONE);

            }
        });
        this.clearNumber.setOnLongClickListener(new Button.OnLongClickListener() {
            public boolean onLongClick(View v) {
                display.setText("");
                clearNumber.setVisibility(View.GONE);
                return true;
            }
        });

    }

    public void onClick(View v) {
        //Clear display if a message is displayed before adding a number


        vibe.vibrate(50);

        Pattern pattern = Pattern.compile("[0-9*#]");
        Matcher matcher = pattern.matcher(this.display.getText().toString());
        if (!matcher.find()) {
            this.display.setText("");
        }

        if (!(text_length > 15))
            switch (v.getId()) {
            case R.id.button0:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_0);
                mp.start();
                this.display.append("0");
                break;
            case R.id.button1:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_1);
                mp.start();
                this.display.append("1");
                break;
            case R.id.button2:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_2);
                mp.start();
                this.display.append("2");
                break;
            case R.id.button3:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_3);
                mp.start();
                this.display.append("3");
                break;
            case R.id.button4:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_4);
                mp.start();
                this.display.append("4");
                break;
            case R.id.button5:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_5);
                mp.start();
                this.display.append("5");
                break;
            case R.id.button6:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_6);
                mp.start();
                this.display.append("6");
                break;
            case R.id.button7:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_7);
                mp.start();
                this.display.append("7");
                break;
            case R.id.button8:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_8);
                mp.start();
                this.display.append("8");
                break;
            case R.id.button9:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_9);
                mp.start();
                this.display.append("9");
                break;
            case R.id.buttonPlus:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_star);
                mp.start();
                this.display.append("+");
                break;
            case R.id.buttonPound:
                if (mp != null && mp.isPlaying())
                    mp.stop();
                mp = MediaPlayer.create(hactivity.getBaseContext(), R.raw.dtmf_hash);
                mp.start();

                this.display.append("#");
                break;
            }
        text_length = display.getText().length();

        if (text_length > 0)
            clearNumber.setVisibility(View.VISIBLE);

    }
}

这是 Logcat:

12-12 16:56:23.921: E/AndroidRuntime(25644): FATAL EXCEPTION: main
12-12 16:56:23.921: E/AndroidRuntime(25644): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.phone91/com.android.phone91.DialerActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.android.phone91.DialPadView
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.os.Looper.loop(Looper.java:155)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.ActivityThread.main(ActivityThread.java:5520)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at java.lang.reflect.Method.invoke(Method.java:511)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at dalvik.system.NativeStart.main(Native Method)
12-12 16:56:23.921: E/AndroidRuntime(25644): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.android.phone91.DialPadView
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:324)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.actionbarsherlock.internal.ActionBarSherlockNative.setContentView(ActionBarSherlockNative.java:119)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.actionbarsherlock.app.SherlockActivity.setContentView(SherlockActivity.java:218)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.android.phone91.DialerActivity.onCreate(DialerActivity.java:24)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.Activity.performCreate(Activity.java:5066)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
12-12 16:56:23.921: E/AndroidRuntime(25644):    ... 11 more
12-12 16:56:23.921: E/AndroidRuntime(25644): Caused by: java.lang.reflect.InvocationTargetException
12-12 16:56:23.921: E/AndroidRuntime(25644):    at java.lang.reflect.Constructor.constructNative(Native Method)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
12-12 16:56:23.921: E/AndroidRuntime(25644):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
12-12 16:56:23.921: E/AndroidRuntime(25644):    ... 24 more
12-12 16:56:23.921: E/AndroidRuntime(25644): Caused by: java.lang.ClassCastException: com.android.phone91.DialerActivity cannot be cast to com.android.phone91.MainActivity
12-12 16:56:23.921: E/AndroidRuntime(25644):    at com.android.phone91.DialPadView.<init>(DialPadView.java:28)
12-12 16:56:23.921: E/AndroidRuntime(25644):    ... 27 more

最佳答案

hactivity = (MainActivity) context;

传递给 DialPadViewcontext 是一个 DialerActivity,但您尝试将其转换为 MainActivity

一般来说, View 不应该依赖于它们所显示的 Activity 。如果您需要编写这样的代码,请退一步并重新考虑设计。在您的情况下,您只需要一个 Context,而不是特定的 Activity - hactivity 的所有用途都在您使用它的地方作为上下文

关于java - 二进制 XML 文件行 #12 : Error inflating class com. android.phone91.DialPadView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20542649/

相关文章:

java - 使用 webAllowOthers 的 H2 控制台远程访问

java - 没有这样的方法依赖异常

java - 带条件的 XML 到 JSON 转换

javascript - jquery:合并 XML 文档

html - 如何在 XML 中编码换行符

java - Android读入与app打包的.txt文件

java - 如何避免空表出现 "SELECT max(rank) FROM test"的空结果?

android - 如何使图像适合我的按钮

java - facebook webhook 验证失败

android - 为什么我的应用的 Google Play 页面不支持某些随机 Android 设备?我已经查看了 list