自定义视图InflateException用于在Fragment中绘制应用程序

时间:2016-02-07 01:00:38

标签: java android xml android-fragments android-studio

我正在尝试使用片段在android工作室内编写触摸屏交互式绘图应用程序;但是,我收到错误但未能找到它是由什么引起的,以及如何修复它。我是应用程序开发的初学者,只是按照教程。请参阅以下链接,如果您需要其他信息,请通知我。非常感谢你提前!

我怀疑它是由xml文件中的包名称或java文件中的构造函数引起的!

问题/错误

  

在   com.app2016.alexker.matchplanning.MatchPlanning.onCreateView(MatchPlanning.java:67)

     

java.lang.RuntimeException:无法启动活动ComponentInfo {com.app2016.alexker.matchplanning / com.app2016.alexker.matchplanning.MainActivity}:   android.view.InflateException:二进制XML文件行#19:错误   inflating class com.codepath.example.simpledrawapp.SimpleDrawingView

Java Class

package com.app2016.alexker.matchplanning;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.util.AttributeSet;

/**
* Created by AlexKer on 16-02-06.
*/
public class SimpleDrawingView extends View {
//set up initial paint color
private final int paintColor = Color.BLACK;
//defines paint and canvas
private Paint drawPaint;
//path
private Path path = new Path();

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, drawPaint);
}

public SimpleDrawingView(Context context){
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    setupPaint();
}

public SimpleDrawingView(Context context, AttributeSet attrs){
    super(context, attrs);
    /*setFocusable(true);
    setFocusableInTouchMode(true);
    setupPaint();*/
}

private void setupPaint() {
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(5);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
}

// Get x and y and append them to the path
public boolean onTouchEvent(MotionEvent event) {
    float pointX = event.getX();
    float pointY = event.getY();
    // Checks for the event that occurs
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // Starts a new line in the path
            path.moveTo(pointX, pointY);
            break;
        case MotionEvent.ACTION_MOVE:
            // Draws line between last point and this point
            path.lineTo(pointX, pointY);
            break;
        default:
            return false;
    }

    postInvalidate(); // Indicate view should be redrawn
    return true; // Indicate we've consumed the touch
}
}

以下是onCreateView

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.fragment_match_planning, container, false);
    return view;
}                                                                                      

和XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_match_plan"
android:orientation="vertical"
android:gravity="center|top"
tools:context="com.app2016.alexker.matchplanning.MatchPlanning"
android:background="@drawable/field">

<!-- TODO: Update blank fragment layout -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.codepath.example.simpledrawapp.SimpleDrawingView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/SimpleDrawingView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

实际上,XML名称中的包名也是错误的。应该 com.app2016.alexker.matchplanning。

我不知道您的观点是什么&尝试使用,但这似乎超级不寻常:

    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true" 

试试这个:

<com.app2016.alexker.matchplanning.SimpleDrawingView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/SimpleDrawingView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

此外 - 目前,LinearLayout或RelativeLayout无用。