5

カスタム ビュー クラスを作成した Androidメイン ライブラリ プロジェクトを作成しました。

package com.my.android.library.layout;

public class CustomView extends View {
...

また、私のスタイルを定義しましたCustomView

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <declare-styleable name="CustomView">
        <attr name="title_text" format="string" />
    </declare-styleable>
</resources>

自分のソース コードを自分のライブラリ プロジェクトを使用している他のユーザーと共有したくないので、分散ライブラリ プロジェクトを作成し、上記のメイン ライブラリ プロジェクトの jar を分散ライブラリ プロジェクトlibs/フォルダに追加し、すべてのリソースをコピーしましたメイン ライブラリ プロジェクトから分散ライブラリ プロジェクトへ。

次に、分散ライブラリ プロジェクトを使用するAndroid アプリ プロジェクトを作成しました。アプリ プロジェクトのメイン レイアウト ファイルで、次のように定義しました。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <com.my.android.library.layout.CustomView
        custom:title_text = "THIS IS TITLE" 
        />
<RelativeLayout>

アプリを実行すると、次の例外が発生しました。

E/AndroidRuntime(30993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.android.app/com.my.android.app.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.my.android.library.layout.CustomView
E/AndroidRuntime(30993):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
E/AndroidRuntime(30993):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
    ...
   Caused by: java.lang.ClassNotFoundException: com.my.android.library.layout.CustomView
E/AndroidRuntime( 2947):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime( 2947):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime( 2947):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime( 2947):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
E/AndroidRuntime( 2947):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)

CustomViewmy in layout xml を膨らませることができないようです。なんで?それを取り除く方法は?

(I checked the main library jar file, there is CustomView class. Please don't just throw me a link of Android website without explanation.)

4

2 に答える 2

-1

このようにしてみましょう:

  public CustomView(Context context) {
    this(context, null, 0);
  }

  public CustomView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public CustomView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    //init your stuff here
  }

例外の完全なスタックトレースを教えてください。

于 2014-08-07T11:56:24.680 に答える