错误:CustomTextView中的android.view.inflateExeption

时间:2017-02-03 19:25:12

标签: android exception casting textview android-inflate

我有一个菜单活动,可以在所有TextView中获取用户设置字体和大小以及文本的行间距,所以我需要一个CustomTextView类来设置这些更改但我的自定义TextView不起作用并在logcat中提供inflate Exception窗口。坦克对你的帮助很大!

我的CustomTextView:

package com.niloo.test2;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {

Context context;
SharedPreferences shp = context.getSharedPreferences("text", Context.MODE_PRIVATE);
int size = shp.getInt("size", 22);
int fasle = shp.getInt("fasle", 1);
String font = shp.getString("font", "bmitra");


public CustomTextView(Context context) {
    super(context);
    if (!isInEditMode())
        setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
        setTextSize(size);
        setLineSpacing(fasle, 1);
}

public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    if (! isInEditMode())
        setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
        setTextSize(size);
        setLineSpacing(fasle, 1);
}

public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    if (!isInEditMode())
        setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));
        setTextSize(size);
        setLineSpacing(fasle, 1);
}

protected void onDraw (Canvas canvas) {
    super.onDraw(canvas);
}
}

我的XML代码:

      <com.niloo.test2.CustomTextView
           android:id="@+id/tv_onv_amoozsh"
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_gravity="center_vertical|center_horizontal"
           android:layout_marginTop="10dp"
           android:layout_weight="0.5"
           android:textColor="@color/sabz_lajani"
           android:textSize="25sp"
           android:textStyle="bold" />

    <com.niloo.test2.CustomTextView
          android:id="@+id/tv_num_amoozsh"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_gravity="center_vertical|center_horizontal"
          android:layout_marginBottom="2dp"
          android:layout_weight="0.75"
          android:textColor="@color/sabz_lajani"
          android:textSize="35sp" />

和我的主要课程:

final CustomTextView tv_onv_amoozsh= (CustomTextView) findViewById(R.id.tv_onv_amoozsh);
        tv_onv_amoozsh.setText("page");
final CustomTextView tv_num_amoozsh=(CustomTextView) findViewById(R.id.tv_num_amoozsh);
        tv_num_amoozsh.setText(sfhe_num);

LogCat错误:

02-03 22:41:38.329: E/AndroidRuntime(19952): FATAL EXCEPTION: main
02-03 22:41:38.329: E/AndroidRuntime(19952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.niloo.test2/com.niloo.test2.MainActivity}: android.view.InflateException: Binary XML file line #143: Error inflating class com.niloo.test2.CustomTextView

3 个答案:

答案 0 :(得分:0)

你在布局的第一个视图中有这个吗?

034

请发布完整的布局文件(将项目中的文件减少到仍然失败的最小值。也许只有一个视图 - 您的自定义文本)

编辑: 您发布的xml代码无效。这是应该工作的固定版本(不要省略“http://”,删除分号)

xmlns:custom="http://schemas.android.com/apk/res-auto"

答案 1 :(得分:0)

问题很可能是您在构造函数之外初始化shpsizefaslefont。这些初始化在任何构造函数代码运行之前发生。永远不会设置context,并且在调用getSharedPreferences时为空。

试试这个:

public class CustomTextView extends TextView {

    private SharedPreferences shp;
    private int size;
    private int fasle;
    private String font;

    public CustomTextView(Context context) {
        super(context);
        init(context);
    }

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

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

    private void init(Context context) {
        if (isInEditMode()) {
            return;
        }
        shp = context.getSharedPreferences("text", Context.MODE_PRIVATE);
        size = shp.getInt("size", 22);
        fasle = shp.getInt("fasle", 1);
        font = shp.getString("font", "bmitra");
        setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/" + font));
        setTextSize(size);
        setLineSpacing(fasle, 1);
    }
}

答案 2 :(得分:0)

错误来自这一行:

var draggable_options = {
  helper: 'clone',
  cursor: 'move',
  revert: 'invalid',
};

$(".ui-draggable").draggable(draggable_options);

$(".ui-droppable").droppable({
  drop: function(event, ui) {
    var $item = ui.draggable;
    $item.draggable(draggable_options)
    $item.attr('style', '')
    $(this).append($item)
  },
  accept: function() {
    return $(this).find("li").length === 0 // Your condition
  }
});

$(".textToImageRightPanel").droppable({
  drop: function(event, ui) {
    var $item = ui.draggable;
    $item.draggable(draggable_options);
    $item.attr('style', '');
    // Return to older place in list
    returnToOlderPlace($item);
  }
});

// Return item by drop in older div by data-tabidx
function returnToOlderPlace($item) {
  var indexItem = $item.attr('data-tabidx');
  var itemList = $(".textToImageRightPanel").find('li').filter(function() {
    return $(this).attr('data-tabidx') < indexItem
  });
  if (itemList.length === 0)
    $("#cardPile").find('ul').prepend($item);
  else
    itemList.last().after($item);
}

此处我们不需要为路径提及关键字&#34; fonts /&#34; ,因为提供路线的 getAssets()方法资产文件夹的路径。所以你可以直接给出如下字体名称:

setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/"+font));