为什么会出现这种异常:java.lang.RuntimeException:输出类时出错

时间:2011-11-29 18:15:47

标签: android exception inflate

我正在尝试开发一个只需打开浏览器到网站的小型Android应用程序。我正在使用mac OS 10.6和最新的android / eclipse工具。但我一直得到这个例外:

  

11-29 13:03:55.113:E / AndroidRuntime(1012):java.lang.RuntimeException:无法启动活动ComponentInfo {com.mycomp / com.mycomp.pack}:android.view.InflateException:二进制XML文件行#7:错误膨胀类com.mycomp.MyTextView

这是我的代码:

package com.mycomp;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class myapp extends Activity {
/** Called when the activity is first created. */`

private MyTextView myview;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      
    Log.d("onCreate", "onCreate ");   

    myview  = (MyTextView)findViewById(R.id.myview);
    myview.setParent(this);
}




public static   class MyTextView extends TextView {
    private View mLastVisChangedView;
    private int mLastChangedVisibility;
    private Activity parent;

    public void setParent(Activity p){
        parent =p;

    }
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public View getLastVisChangedView() {
        return mLastVisChangedView;
    }

    public int getLastChangedVisibility() {
        return mLastChangedVisibility;
    }

    @Override
    protected void  onVisibilityChanged(View changedView, int visibility){
        Log.d("onVisibilityChanged", "new vis == " + visibility);
        if (parent !=null && visibility == View.VISIBLE){
            parent.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.mysite.com")));
            parent.moveTaskToBack(true);
        }
    }
}

}

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

<com.mycomp.MyTextView
     android:id="@+id/myview"
      android:text="@string/hello"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

类型为com.mycomp.myapp.MyTextView。 (或$,因为它是一个内部类?)

您可能还需要按如下方式指定视图:

<view class="com.mycomp.myapp$MyEditText" ...

在这种情况下,我认为你不需要onFinishInflate

答案 1 :(得分:0)

您应该将MyTextView类放在单独的文件中。那么com.mycomp.MyTextView就是正确的。