我在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>


Dialpadview:

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;


传递给contextDialPadViewDialerActivity,但是您尝试将其强制转换为MainActivity

通常,视图不应依赖于它们所显示的活动。如果您需要编写这样的代码,请退后一步,重新思考设计。就您而言,您只需要一个Context,而不是一个特定的Activity-hactivity的所有用法都在您将其用作Context的地方。

关于java - 二进制XML文件第12行:膨胀类com.android.phone91.DialPadView时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20542649/

10-11 23:02