Java 片段中绘图应用程序的自定义视图充气异常
我试图用一段代码在android工作室内编写一个触摸屏交互式绘图应用程序;但是,我收到一个错误,但未能找到它是由什么引起的以及如何修复它。我是一个应用程序开发的初学者,只是在学习教程。请参阅下面的链接,如果您需要更多信息,请通知我。提前非常感谢 我怀疑这是由xml文件中的包名或java文件中的构造函数引起的 问题/错误 在 com.app2016.alexker.matchplanning.matchplanning.onCreateView(matchplanning.java:67) java.lang.RuntimeException:无法启动活动组件信息{com.app2016.alexker.matchplanning/com.app2016.alexker.matchplanning.MainActivity}: android.view.InflateException:二进制XML文件行#19:错误 膨胀类com.codepath.example.simpledrawapp.SimpleDrawingView Java类Java 片段中绘图应用程序的自定义视图充气异常,java,android,xml,android-fragments,android-studio,Java,Android,Xml,Android Fragments,Android Studio,我试图用一段代码在android工作室内编写一个触摸屏交互式绘图应用程序;但是,我收到一个错误,但未能找到它是由什么引起的以及如何修复它。我是一个应用程序开发的初学者,只是在学习教程。请参阅下面的链接,如果您需要更多信息,请通知我。提前非常感谢 我怀疑这是由xml文件中的包名或java文件中的构造函数引起的 问题/错误 在 com.app2016.alexker.matchplanning.matchplanning.onCreateView(matchplanning.java:67) jav
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 我不知道您试图使用的视图是什么,但这似乎非常不寻常:
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是无用的。实际上,包名在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没有用。应用程序已成功启动,谢谢,但每当我尝试使用画笔绘制时,它都会崩溃并显示“不幸的是,MatchPlanning已停止”。你知道是什么引起的吗?非常感谢你!这是另一个问题。你应该打开一个新的线程。问题是:java.lang.NullPointerException:尝试从空对象引用上的字段“long android.graphics.Paint.mNativePaint”读取,如果取消注释,则可能会运行该字段:setupPaint();在你的第二个构造函数中。是的,我知道了,我还有最后一个问题:是否需要两个独立的构造函数,或者我可以使用第二个同时包含上下文和属性的构造函数?谢谢!您需要第二个构造函数。它用于从XML(第二个参数)传递属性。按住control或command并在任一构造函数中单击super(),然后阅读文档。该应用程序已成功启动,谢谢,但每当我尝试使用画笔绘制时,它都会崩溃并显示“不幸的是,MatchPlanning已停止”。你知道是什么引起的吗?非常感谢你!这是另一个问题。你应该打开一个新的线程。问题是:java.lang.NullPointerException:尝试从空对象引用上的字段“long android.graphics.Paint.mNativePaint”读取,如果取消注释,则可能会运行该字段:setupPaint();在你的第二个构造函数中。是的,我知道了,我还有最后一个问题:是否需要两个独立的构造函数,或者我可以使用第二个同时包含上下文和属性的构造函数?谢谢!您需要第二个构造函数。它用于从XML(第二个参数)传递属性。按住control或command并单击任一构造函数中的super(),然后阅读文档。