如何在另一个活动中将用户图形保存到SQLite数据库?

时间:2019-05-17 13:36:23

标签: android android-sqlite bitmapimage

我有一个类扩展了View并在那做我的画。完成画后,我从菜单中打开另一个活动。在第二个活动中,有用于用户名的editText字段,还有一个保存按钮。我想将它们保存到我的SQLite数据库中,首先保存图形,然后保存与用户相关的图像。我怎样才能做到这一点 ?现在这是我的当前代码,出现错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context com.example.mydrawingapp.View.AppView.getContext()' on a null object reference
        at com.example.mydrawingapp.SecondActivity$1.onClick(SecondActivity.java:31)

我的数据库助手类:

   public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "drawingApp.db";
            public static final String TABLE_NAME="saveUser";
    public static final String COL_1 = "ID";
    public static final String COL_2="username";
    public static final String COL_3="image";


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null,1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE saveUser (ID INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT,image BLOB)");
    }

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

    public long addImage(String user, byte[] image){
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("username",user);
        contentValues.put("image",image);
        long res = database.insert("saveUser",null,contentValues);
        database.close();
        return res;
    }
}

这是我的第二个活动类中的代码:

        public class SecondActivity extends AppCompatActivity {
    private EditText textUsername;
    private Button buttonRegister;
    private String username;
    private AppView appView;
    private DatabaseHelper dbHelper;
    private  byte[] image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        textUsername = (EditText) findViewById(R.id.editText);
        username = textUsername.getText().toString();
        buttonRegister=(Button) findViewById(R.id.saveButton);

        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                appView= new AppView(appView.getContext(),null );
                image = appView.saveImage();
                dbHelper.addImage(username,image);
            }
        });
    }
}

主要活动:

   public class MainActivity extends AppCompatActivity {
    private AppView appView;
    private AlertDialog.Builder currentAlertDialog;
    private ImageView widthImageView;
    private AlertDialog dialogLineWidth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appView = findViewById(R.id.view);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);


        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.clearId:
                appView.clear();
                break;
            case R.id.saveId:
                openLoginDialog();
                break;

            case R.id.lineWidth:
                showLineWidthDialog();
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    private void openLoginDialog() {
        Intent intent = new Intent(this,SecondActivity.class);
        startActivity(intent);
    }

    void showLineWidthDialog() {
        currentAlertDialog = new AlertDialog.Builder(this);
        View view = getLayoutInflater().inflate(R.layout.width_dialog, null);
        final SeekBar widthSeekBar = view.findViewById(R.id.widthSeekBar);
        Button setLineWidthButton = view.findViewById(R.id.widthDialogButton);
        widthImageView = view.findViewById(R.id.imageViewId);
        setLineWidthButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                appView.setLineWidth(widthSeekBar.getProgress());
                dialogLineWidth.dismiss();
                currentAlertDialog = null;

            }
        });


        widthSeekBar.setOnSeekBarChangeListener(widthSeekBarChange);


        currentAlertDialog.setView(view);
        dialogLineWidth = currentAlertDialog.create();
        dialogLineWidth.setTitle("Set Line Width");
        dialogLineWidth.show();
    }

    SeekBar.OnSeekBarChangeListener widthSeekBarChange = new SeekBar.OnSeekBarChangeListener() {
        Bitmap bitmap = Bitmap.createBitmap(400, 100, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);



        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            Paint p = new Paint();
            p.setColor(appView.getDrawingColor());
            p.setStrokeCap(Paint.Cap.ROUND);
            p.setStrokeWidth(progress);
            bitmap.eraseColor(Color.WHITE);
            canvas.drawLine(30, 50, 370, 50, p);
            widthImageView.setImageBitmap(bitmap);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    };

}

2 个答案:

答案 0 :(得分:1)

您的context为空,因此您需要通过以下方式进行初始化:

   public class SecondActivity extends AppCompatActivity {
private EditText textUsername;
private Button buttonRegister;
private String username;
private AppView appView;
private DatabaseHelper dbHelper;
private  byte[] image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    textUsername = (EditText) findViewById(R.id.editText);
    username = textUsername.getText().toString();
    buttonRegister=(Button) findViewById(R.id.saveButton);

    buttonRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            appView= new AppView(SecondActivity.this ,null ); // change this line
            image = appView.saveImage();
            dbHelper.addImage(username,image);
        }
    });
}

答案 1 :(得分:0)

buttonRegister.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        appView= new AppView(**appView**.getContext(),null );
        image = appView.saveImage();
        dbHelper.addImage(username,image);
    }

appView <-在点击时此属性为空