为android中的自定义视图增加方法的异常

时间:2013-03-08 13:43:00

标签: android

我正在编写用于android的自定义控件,我对下一个问题感到困惑: 当我尝试调用View.inflate()静态方法时(请查看下面的代码),我得到了运行时异常android.view.InflateException。 我的观点有代码

public class NavigationView extends RelativeLayout implements NewRadioConst {
      private TextView centerLabel;
      private Context context;
      private Typeface font;
      private ImageView leftArrow;
      private TextView leftLabel;
      private View leftWrapper;
      private ImageView rightArrow;
      private TextView rightLabel;
      private View rightWrapper;

      public NavigationView(Context paramContext)
      {
        this(paramContext, null);
      }

      public NavigationView(Context paramContext, AttributeSet paramAttributeSet)
      {
        this(paramContext, paramAttributeSet, 0);
      }

      public NavigationView(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
      {
        super(paramContext, paramAttributeSet, paramInt);
        this.context = paramContext;
        View.inflate(context, R.id.navView1, this);  //this line take the exception
      }

}

这是我尝试膨胀的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navView1" >
    <FrameLayout android:id="@+id/centerWrapper" android:layout_centerInParent="true" style="@style/WrapContent">
        <ImageView android:layout_gravity="center" android:id="@+id/centerBg" android:layout_width="90.0dip" android:layout_height="30.0dip" android:src="@drawable/center_bg" />
        <TextView android:layout_gravity="center" android:id="@+id/centerLabel" android:text="@string/music" style="@style/TextLabel" />
    </FrameLayout>
    <LinearLayout android:id="@+id/leftWrapper" android:clickable="true" android:layout_centerVertical="true" style="@style/WrapContent">
        <TextView android:textSize="12.0dip" android:layout_gravity="center_vertical" android:id="@+id/leftLabel" android:layout_marginLeft="8.0dip" android:text="@string/info" android:singleLine="true" style="@style/TextLabel" />
        <ImageView android:layout_gravity="center_vertical" android:id="@+id/leftArrow" android:layout_width="50.0dip" android:layout_height="30.0dip" android:src="@drawable/left_arrow" />
    </LinearLayout>
    <LinearLayout android:id="@+id/rightWrapper" android:clickable="true" android:layout_centerVertical="true" style="@style/WrapContent">
        <ImageView android:layout_gravity="center_vertical" android:id="@+id/rightArrow" android:layout_width="50.0dip" android:layout_height="30.0dip" android:src="@drawable/right_arrow" />
        <TextView android:textSize="12.0dip" android:layout_gravity="center_vertical" android:id="@+id/rightLabel" android:layout_marginRight="8.0dip" android:text="@string/news" android:singleLine="true" style="@style/TextLabel" />
    </LinearLayout>

</RelativeLayout>

这是logcat

03-08 11:37:00.585: E/AndroidRuntime(2374): FATAL EXCEPTION: main
03-08 11:37:00.585: E/AndroidRuntime(2374): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.groupab.newradio/com.groupab.newradio.MusicActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class com.groupab.newradio.view.NavigationView
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.os.Looper.loop(Looper.java:123)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at java.lang.reflect.Method.invoke(Method.java:507)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at dalvik.system.NativeStart.main(Native Method)
03-08 11:37:00.585: E/AndroidRuntime(2374): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class com.groupab.newradio.view.NavigationView
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.createView(LayoutInflater.java:518)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.Activity.setContentView(Activity.java:1657)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at com.polontech.android.newradio.MusicActivity.onCreate(MusicActivity.java:12)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
03-08 11:37:00.585: E/AndroidRuntime(2374):     ... 11 more
03-08 11:37:00.585: E/AndroidRuntime(2374): Caused by: java.lang.reflect.InvocationTargetException
03-08 11:37:00.585: E/AndroidRuntime(2374):     at java.lang.reflect.Constructor.constructNative(Native Method)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.view.LayoutInflater.createView(LayoutInflater.java:505)
03-08 11:37:00.585: E/AndroidRuntime(2374):     ... 21 more
03-08 11:37:00.585: E/AndroidRuntime(2374): Caused by: java.lang.RuntimeException: native typeface cannot be made
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.graphics.Typeface.<init>(Typeface.java:147)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at android.graphics.Typeface.createFromAsset(Typeface.java:121)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at com.groupab.newradio.view.NavigationView.<init>(NavigationView.java:41)
03-08 11:37:00.585: E/AndroidRuntime(2374):     at com.groupab.newradio.view.NavigationView.<init>(NavigationView.java:33)
03-08 11:37:00.585: E/AndroidRuntime(2374):     ... 24 more

1 个答案:

答案 0 :(得分:0)

inflate方法期望表单的布局ID:R.layout.XXX。我将从顶级元素中删除id标识符navView1,并引用xml布局文件的名称。因此,如果您将xml布局文件命名为navView1,则可以简单地引用为R.layout.navView1。