膨胀布局时出错

时间:2016-07-27 20:53:31

标签: java android android-view

我已经创建了一个应用,但是当我在调用setContentView(R.layout.points)

时使用on create方法
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.points);
        final CanvasView canvas = (CanvasView) findViewById(R.id.signature_canvas);
      //  setContentView(R.layout.activity_main);


     //  addTouchListener();

        }

CanvasView.java

package com.example.android.touchview;

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

import java.util.List;

/**
 * Created by Vikas on 7/26/2016.
 */
public class CanvasView extends View
{

    private Paint mPaint = new Paint();


    DbActivity db =new DbActivity();
    List mPts=db.getData();
    float [] points =new float[mPts.size()];

    public CanvasView(Context context,AttributeSet attributeSet)
    {

        super(context,attributeSet);
       // Paint paint = mPaint;

        //canvas.translate(10, 10);

        //canvas.drawColor(Color.WHITE);

        //mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(0);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(3);

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawPoints(points, mPaint);
    }


}

这是我的xml文件

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

    <com.example.android.touchview.CanvasView
        android:id="@+id/signature_canvas"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</FrameLayout>

DbActivity.java

package com.example.android.touchview;

/**
 * Created by Vikas on 7/20/2016.
 */

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import java.sql.SQLException;
import java.util.ArrayList;

public class DbActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_db);
        TextView tv = (TextView) findViewById(R.id.SQLinfo);
        DataBase db = new DataBase(this);
        try {
            db.open();
            ArrayList<Float> data = db.getData();
            db.close();


           // data.toString();
            tv.setText(data.toString());
            //Log.d("THE DATA IS ", "+ data.get(i)");

         /*  for(int i=0;i<data.size();i++)
            {
                tv.setText(data.get(i).toString());
                Log.d("hi", "data[i]");
            }*/

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Float> getData()
    {
        ArrayList<Float> data= new ArrayList<>();
        DataBase db = new DataBase(this);
        try {
            db.open();
             data = db.getData();
            db.close();


    } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return data;
    }
}

修改了Canvas.java

package com.example.android.touchview;

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

import java.util.List;

/**
 * Created by Vikas on 7/26/2016.
 */
public class CanvasView extends View
{

    private Paint mPaint = new Paint();
    float[] points ;




    public CanvasView(Context context,AttributeSet attributeSet)
    {

        super(context,attributeSet);
        DbActivity db =new DbActivity();
        List mPts=db.getData();
         points =new float[mPts.size()];

       // Paint paint = mPaint;

        //canvas.translate(10, 10);

        //canvas.drawColor(Color.WHITE);

        //mPaint.setColor(Color.RED);

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        mPaint.setStrokeWidth(0);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(3);

        canvas.drawPoints(points, mPaint);
    }


}

Database.java

package com.example.android.touchview;

/**
 * Created by Vikas on 7/20/2016.
 */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.sql.SQLException;
import java.util.ArrayList;


public class DataBase
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_X = "X";
    public static final String KEY_Y = "Y";

    private static final String DATABASE_NAME = "Details";
    private static final String DATABASE_TABLE = "DetailsTable";
    private static final int DATABASE_VERSION = 1;

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;



    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_X + " TEXT, " + KEY_Y + " TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }
    }

    public DataBase(Context c) {
        ourContext = c;
    }

    public DataBase open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        ourHelper.close();
    }

    public long createEntry(String x, String y) {
        ContentValues cv = new ContentValues();
        cv.put(KEY_X, x);
        cv.put(KEY_Y, y);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public ArrayList<Float> getData() {
        ourDatabase = ourHelper.getReadableDatabase();
        Point p = new Point();
        ArrayList<Float> dataPoints = new ArrayList<>();
        String[] col = new String[]{KEY_ROWID, KEY_X, KEY_Y};
        Cursor c = ourDatabase.query(DATABASE_TABLE, col, null, null, null, null, null);
        float[] array = new float[c.getCount()];
        float[] array1 = new float[c.getCount()];
        int i = 0;
        if (c.moveToFirst())
        {
            do {
                float x = c.getFloat(c.getColumnIndex("X"));
                p.setX(x);
                dataPoints.add(p.getX());

                float y = c.getFloat(c.getColumnIndex("Y"));
                p.setY(y);
                dataPoints.add(p.getY());
                array[i] = x;
                array1[i] = y;
                i++;
            } while (c.moveToNext());
        }


        Log.d("hi", "getData() called");
        return dataPoints;
    }
}

我收到此错误

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.touchview/com.example.android.touchview.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.example.android.touchview.CanvasView

1 个答案:

答案 0 :(得分:0)

以下行似乎导致错误:

DbActivity db =new DbActivity();
List mPts=db.getData();
float [] points =new float[mPts.size()];

首先,将所有初始化移至构造函数方法并避免这种初始化方式。其次,你不应该实例化一个活动。

建议将所有数据库级逻辑移动到名为DbHelper的类,并在方法范围中使用它。这样的事情看起来很好:

DbHelper db;
List mPts;
float [] points;
public CanvasView(Context context,AttributeSet attributeSet)
{
    super(context,attributeSet);

    db = new DbHelper(...);
    mPts = new ArrayList<>();
    points = new float[...];
    ...
}