我的应用程序停止,并在第0行显示二进制XML文件错误。有点奇怪,因为我觉得0行没有问题,所以我需要你们的帮助,大家。谢谢!
这在大于20的api上可以正常工作,只是比api设备导致应用程序崩溃要低
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vickyy.iwasframed, PID: 7818
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vickyy.iwasframed/com.example.vickyy.iwasframed.ImageSize}: android.view.InflateException: Binary XML file line #0: Error inflating class com.example.vickyy.iwasframed.CustomFrame
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.example.vickyy.iwasframed.CustomFrame
at android.view.LayoutInflater.createView(LayoutInflater.java:609)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.vickyy.iwasframed.ImageSize.onCreate(ImageSize.java:31)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getConstructor(Class.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:574)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.vickyy.iwasframed.ImageSize.onCreate(ImageSize.java:31)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
I/处理:发送信号。PID:7818信号:9
应用程序已终止。
这是xmlfile
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/AppColor"
tools:context="com.example.vickyy.iwasframed.FrameActivity">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/toolbar"
android:background="@color/ActionBarColor">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ActionBarColor">
<Button
android:id="@+id/btnDone"
android:layout_width="45dp"
android:layout_height="30dp"
android:text="Done"
android:layout_centerVertical="true"
android:textColor="@android:color/white"
android:backgroundTint="@color/ButtonColor"
android:layout_alignParentLeft="true"
android:textSize="8sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="USE IMAGE SIZE"
android:textColor="@android:color/white"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textStyle="bold"
android:textSize="16sp"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="WIDTH:"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HEIGHT:"
android:layout_weight="1"
android:layout_marginLeft="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MAT OVERLAP:"
android:layout_weight="1"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">
<EditText
android:layout_weight="1"
android:id="@+id/edtWidth"
android:layout_width="match_parent"
android:layout_height="25dp"
android:paddingLeft="10dp"
android:layout_toLeftOf="@+id/txtX"
android:textSize="10sp"
android:textStyle="bold"
android:textCursorDrawable="@null"
android:background="@drawable/custom_edittext"
android:inputType="numberDecimal"
android:layout_marginRight="10dp">
</EditText>
<EditText
android:layout_weight="1"
android:id="@+id/edtHeight"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_toRightOf="@+id/txtX"
android:textCursorDrawable="@null"
android:layout_marginLeft="10dp"
android:textSize="10sp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:inputType="numberDecimal"
android:background="@drawable/custom_edittext"
android:layout_alignParentRight="true">
</EditText>
<EditText
android:layout_weight="1"
android:id="@+id/edtOverlap"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_toRightOf="@+id/txtX"
android:textCursorDrawable="@null"
android:layout_marginLeft="10dp"
android:textSize="10sp"
android:paddingLeft="10dp"
android:textStyle="bold"
android:inputType="numberDecimal"
android:background="@drawable/custom_edittext"
android:layout_alignParentRight="true"></EditText>
</LinearLayout>
</RelativeLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout">
<com.example.vickyy.iwasframed.CustomFrame
android:id="@+id/imgFrame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/imgView"
android:layout_width="255dp"
android:layout_height="360dp"
android:scaleType="fitXY"
android:layout_gravity="center"
android:src="@drawable/flower"
/>
</FrameLayout>
这是一个自定义类,我为用double和triple mat绘制矩形编写了代码
public class CustomFrame extends AppCompatImageView{
Paint paint;
int top,left,right,bottom;
Context context;
public CustomFrame(Context context) {
super(context);
paint = new Paint();
this.context=context;
}
CustomFrame(Context context, AttributeSet attrs) {
super(context, attrs);
}
CustomFrame(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
SharedPreferences sharedPreferences = getContext().getSharedPreferences("Params", Context.MODE_PRIVATE);
int color = sharedPreferences.getInt("color",R.color.black);
int canvasW = getWidth();
int canvasH = getHeight();
Point centerOfCanvas = new Point(canvasW / 2, canvasH / 2);
int width = sharedPreferences.getInt("width",0);
int height = sharedPreferences.getInt("height",0);
int overLap=sharedPreferences.getInt("OverLap",0);
boolean start=sharedPreferences.getBoolean("start",false);
int rectW = width*75; //convert to pixel
int rectH = height*75;
left = centerOfCanvas.x - (rectW / 2);
top = centerOfCanvas.y - (rectH / 2);
right = centerOfCanvas.x + (rectW / 2);
bottom = centerOfCanvas.y + (rectH / 2);
paint = new Paint();
paint.setColor(color);
paint.setStyle(Paint.Style.STROKE);
/* if(start) {
paint.setStrokeWidth(20);
}
else{
}*/
paint.setStrokeWidth(overLap);
int l,t,r,b;
int tripleLeft,tripleTop,tripleRight,tripleBottom;
boolean doubleMat = sharedPreferences.getBoolean("DoubleMat",false);
boolean tripleMat = sharedPreferences.getBoolean("TripleMat",false);
if(doubleMat){
int matHeight = sharedPreferences.getInt("MatHeight",0);
int matWidth = sharedPreferences.getInt("MatWidth",0);
int thickness=sharedPreferences.getInt("Doublethickness",0);
int matW = (width)*75;
int matH = (height)*75;
int matw=matW-overLap;
int math=matH-overLap;
l = centerOfCanvas.x - (matw / 2);
t = centerOfCanvas.y - (math / 2);
r = centerOfCanvas.x + (matw / 2);
b = centerOfCanvas.y + (math / 2);
Paint paint1 = new Paint();
paint1.setColor(color);
paint1.setStyle(Paint.Style.STROKE);
paint1.setStrokeWidth(thickness); //inside
paint.setStrokeWidth(overLap); //mat
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("MatHeight",math);
editor.putInt("MatWidth",matw);
editor.commit();
canvas.drawRect(left, top, right, bottom, paint);
canvas.drawRect(l, t, r, b, paint1);
}
else if(tripleMat){
int matHeight = sharedPreferences.getInt("MatHeight",0);
int matWidth = sharedPreferences.getInt("MatWidth",0);
int dThickness=sharedPreferences.getInt("Doublethickness",0);
int tThickness=sharedPreferences.getInt("Triplethickness",0);
int matW = (width)*75; //-2 here
int matH = (height)*75;
int matw=matW-overLap;
int math=matH-overLap;
l = centerOfCanvas.x - (matw / 2);
t = centerOfCanvas.y - (math / 2);
r = centerOfCanvas.x + (matw / 2);
b = centerOfCanvas.y + (math / 2); //inside
int tripleWidth=(width)*75;
int tripleHeight=(height)*75;
int tmatw=tripleWidth-dThickness-overLap;
int tmath=tripleHeight-dThickness-overLap;
tripleLeft=centerOfCanvas.x-(tmatw/2);
tripleTop=centerOfCanvas.y-(tmath/2);
tripleRight=centerOfCanvas.x+(tmatw/2);
tripleBottom=centerOfCanvas.y+(tmath/2);
Paint triplePaint=new Paint();
triplePaint.setColor(color);
triplePaint.setStyle(Paint.Style.STROKE);
triplePaint.setStrokeWidth(overLap);
Paint paint1 = new Paint();
paint1.setColor(color);
paint1.setStyle(Paint.Style.STROKE);
paint1.setStrokeWidth(dThickness); //inside
paint.setStrokeWidth((int)tThickness); //mat
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("MatHeight",tmath);
editor.putInt("MatWidth",tmatw);
editor.commit();
canvas.drawRect(left, top, right, bottom, triplePaint); //outer
canvas.drawRect(l, t, r, b, paint1); //inside
canvas.drawRect(tripleLeft,tripleTop,tripleRight,tripleBottom,paint); //outside
}
else {
/* Rect r=new Rect(left,top,right,bottom);
//left
canvas.drawLine(r.left,r.top,r.left,r.bottom,paint);
//top
canvas.drawLine(r.left,r.top,r.right,r.top,paint2);
//right
canvas.drawLine(r.right,r.top,r.right,r.bottom,paint);
//bottom
canvas.drawLine(r.left,r.bottom,r.right,r.bottom,paint2);*/
// canvas.drawLine();
// String text=String.valueOf(overLap);
canvas.drawRect(left, top, right, bottom, paint);
// canvas.drawText(text,left,bottom,paint);
}
}
}
最佳答案
你的构造函数必须是公共的。膨胀时无法访问私有构造函数。尤其是2变量1(您的1变量是目前唯一的公共变量)。
关于android - 二进制XML文件第0行:错误夸大类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47363407/