2014-10-16 111 views
0

我想添加一个ListView和自定义视图在我的布局,但我总是得到这个错误:安卓:自定义视图在XML的布局

” ...错误充气类com。示例.. 。“

没有自定义视图它的作品,只显示ListView。


活动:

package com.example.training; 

public class PracticeActivity extends ActionBarActivity{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.practice_layout); 
     initListView(); //private function 
    } 
    //Other stuff 
} 

自定义视图:

package com.example.training; 

public class LinearChart extends View{ 

    LinearChart(Context context, AttributeSet attrs){ 
     super(context,attrs); 
    } 

    LinearChart(Context context){ 
     super(context); 
    } 

    LinearChart(Context context, AttributeSet attrs, int defStyle){ 
     super(context,attrs, defStyle); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     canvas.drawColor(Color.WHITE); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(10); 
     canvas.drawLine(10, 10, 30, 30, paint); 
    } 
} 

XML的布局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ListView 
     android:id="@+id/practice_listview" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:layout_centerHorizontal="true" > 
    </ListView> 

    <view 
     android:id="@+id/view1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/practice_listview" 
     class="com.example.training.LinearChart" /> 

</RelativeLayout> 
+0

确保清理项目以刷新生成的文件。 – 2014-10-16 10:15:56

回答

1

你班上没有公共构造函数。确保你重写构造函数。它应该是这样的,而不是:

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

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

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

通知public改性剂的成瘾性。

+0

不能相信这是解决方案:D非常感谢 – S1J0 2014-10-16 10:36:13

+0

您可以检查您的logcat以获取更多信息。通常这会抛出'引发:java.lang.NoSuchMethodException: [类的android.content.Context,接口android.util.AttributeSet]'这告诉你,没有找到构造函数。默认访问修饰符是私人的,所以'LayoutInflater'找不到它。祝你有美好的一天。 – 2014-10-16 10:40:17

0
<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ListView 
    android:id="@+id/practice_listview" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_centerHorizontal="true" > 
</ListView> 

<com.example.training.LinearChart 
    android:id="@+id/view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/practice_listview" 
    /> 

    </RelativeLayout> 
+1

与OP的代码完全相同。 – 2014-10-16 10:14:50

+0

给我看例外。但首先清理该项目,并通过将我的代码放入xml来重建它。 – 2014-10-16 10:17:51

0

而不是<view在您的xml文件中,您需要该类的完全限定名称(或使用新的xml名称空间)。所以,在这个例子中,你的XML可能看起来像:

<com.example.training.LinearChart 
    android:id="@+id/view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

(因此删除类:属性,在当前的XML)。然后这应该膨胀正确。

+1

这是一回事。你不需要这样做。 – 2014-10-16 10:24:31