android.view.InflateException-创建自定义视图的问题 [英] android.view.InflateException - Issues creating custom view

查看:76
本文介绍了android.view.InflateException-创建自定义视图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android开发的新手,真的很难使用自定义视图.我已经阅读了很多有关SO的内容,它对其他所有方面都非常有帮助,我似乎无法解决这一问题.你们很聪明,非常了解您的东西.我希望有一天可以退还SO,而不是总是服用!任何帮助将不胜感激.谢谢.

I'm new to android development and really struggling with using a custom view. I've read a lot of stuff on SO and its been really helpful with everything else, I just cant seem to work this one out. You guys are very smart and know your stuff so well. I hope to one day give back on SO as opposed to always taking! Any help would be very much appreciated. Thanks.

activity_display_message.xml

activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/share"
    />
    <view class="me.myfirstapp.DisplayMessageActivity$MyView"
        android:layout_width="150dp"
        android:layout_height="100dp"
    />

DisplayMessageActivity.java

DisplayMessageActivity.java

package me.myfirstapp;
import android.os.Bundle; 
import android.app.Activity;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ShareActionProvider;
import android.support.v4.app.NavUtils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Build;

@SuppressLint({ "NewApi", "DrawAllocation" }) public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

public class MyView extends View {

    private int mWidth;
    private int mHeight;

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

    public MyView(Context context, AttributeSet attribs) {
        super(context, attribs);
    }

    public MyView(Context context, AttributeSet attribs, int defStyle) {
        super(context, attribs, defStyle);
    }

    @Override        
    protected void onDraw(Canvas canvas) {

       super.onDraw(canvas);
       Paint paint = new Paint();

       Bitmap b1=BitmapFactory.decodeResource(getResources(), R.drawable.logolong);
       b1 = Bitmap.createScaledBitmap(b1, 100, 20, false);
       canvas.drawBitmap(b1, 600, 880, paint);

       Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/regencielight.TTF");
       paint.setTypeface(tf);
       paint.setColor(Color.parseColor("#ffffff"));
       paint.setTextSize(46);
       canvas.drawText(message, x1, 880, paint);

       }  

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
            mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
            setMeasuredDimension(mWidth, mHeight);
        }

   }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

logcat输出

03-15 21:14:23.382: E/AndroidRuntime(25443): FATAL EXCEPTION: main
03-15 21:14:23.382: E/AndroidRuntime(25443): Process: me.myfirstapp, PID: 25443
03-15 21:14:23.382: E/AndroidRuntime(25443): java.lang.RuntimeException: Unable to start     activity ComponentInfo{me.myfirstapp/me.myfirstapp.DisplayMessageActivity}:     android.view.InflateException: Binary XML file line #12: Error inflating class     me.myfirstapp.DisplayMessageActivity$MyView
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.os.Handler.dispatchMessage(Handler.java:102)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.os.Looper.loop(Looper.java:136)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.main(ActivityThread.java:5017)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.reflect.Method.invokeNative(Native Method)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.reflect.Method.invoke(Method.java:515)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at dalvik.system.NativeStart.main(Native Method)
03-15 21:14:23.382: E/AndroidRuntime(25443): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class me.myfirstapp.DisplayMessageActivity$MyView
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.createView(LayoutInflater.java:603)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.Activity.setContentView(Activity.java:1929)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at me.myfirstapp.DisplayMessageActivity.onCreate(DisplayMessageActivity.java:44)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.Activity.performCreate(Activity.java:5231)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-15 21:14:23.382: E/AndroidRuntime(25443):    ... 11 more
03-15 21:14:23.382: E/AndroidRuntime(25443): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.Class.getConstructorOrMethod(Class.java:472)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at java.lang.Class.getConstructor(Class.java:446)
03-15 21:14:23.382: E/AndroidRuntime(25443):    at android.view.LayoutInflater.createView(LayoutInflater.java:568)
03-15 21:14:23.382: E/AndroidRuntime(25443):    ... 22 more

推荐答案

如果您将内部类用作自定义视图类,则它必须是 static .充气器没有引用外部类来实例化它.

If you're using an inner class as a custom view class, it must be static. The inflater doesn't have a reference to the outer class to instantiate it.

最好将自定义视图类单独放置,而不要将其作为内部类.

It's better to have your custom view classes on their own and not as inner classes.

这篇关于android.view.InflateException-创建自定义视图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆