lost baby
lost baby

Reputation: 3268

why this exception: java.lang.RuntimeException: Error inflating class

I am trying to develop a tiny android app that just opens a browser to a website. I am using mac OS 10.6 and the latest android/eclipse tools. But I keep getting this exception:

11-29 13:03:55.113: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycomp/com.mycomp.pack}: android.view.InflateException: Binary XML file line #7: Error inflating class com.mycomp.MyTextView

Here is my code:

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>

Upvotes: 0

Views: 524

Answers (2)

Dave Newton
Dave Newton

Reputation: 160251

The type would be com.mycomp.myapp.MyTextView. (Or $ since it's an inner class?)

You may also need to specify the view as follows:

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

I don't think you need an onFinishInflate in this case.

Upvotes: 1

Sarel Botha
Sarel Botha

Reputation: 12700

You should put the MyTextView class in a separate file. Then com.mycomp.MyTextView would be correct.

Upvotes: 0

Related Questions