java - fragment 中绘图应用程序的自定义 View InflateException

标签 java android xml android-fragments android-studio

我正在尝试使用 fragment 在 Android 工作室内编写触摸屏交互式绘图应用程序;但是,我收到一个错误,但无法找到其原因以及如何修复它。我是应用程序开发的初学者,只是遵循教程。请参阅下面的链接,如果您需要更多信息,请通知我。提前非常感谢!

我怀疑是xml文件中的包名或者java文件中的构造函数导致的!

问题/错误

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

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app2016.alexker.matchplanning/com.app2016.alexker.matchplanning.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class com.codepath.example.simpledrawapp.SimpleDrawingView

Java 类

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>

最佳答案

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

我不知道您要使用的 View 是什么,但这看起来非常不寻常:

    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 没有用。

关于java - fragment 中绘图应用程序的自定义 View InflateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248515/

相关文章:

java - 在回调外部更新变量

java - 外键问题MySQL

css - 有没有办法在渲染 XML + CSS 时避免 IE7 怪癖模式?

javascript - 如何使用 XSL 属性传递两个或多个参数

java:关闭子进程标准流?

java - 如何在 SharedPreferences 中存储和检索 Location 对象?

java - 如何在 Java 中使用单选按钮

android - 通过在窗口部件上拖放来更改 android Activity 的窗口大小

android - Android 中的 HelloMapView

c# - C# webservice 和/或 xpath 查询的 xml 输出问题