更新SQLite数据库行时以及Flash上​​一个活动打开后,应用程序活动崩溃

时间:2017-04-17 06:43:45

标签: android sqlite flash exception android-sqlite

我的第一个第一个活动是插入数据,然后转到第二个活动 第二个活动使用“A”更新插入的行,然后转到第三个活动 现在,....第三个活动活动使用 B 更新同一行....但是此活动在闪存和第二个活动再次打开后关闭,并且除了具有不同数字的PID之外没有显示任何内容每次和 SIG:9 ,每次都相同.......更新和插入的代码在这里

        public boolean updateForm1(String fake,String Res, String Gen,String rooms){

    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues =new ContentValues();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        if(Objects.equals(fake, "A")){
        contentValues.put(CoL_4,Res);
        contentValues.put(CoL_5,Gen);
        contentValues.put(CoL_6,rooms);
            db.update(TABLE_NAME,contentValues, "Residence = ?",new String[] { fake });
            db.close();}
        else if(Objects.equals(fake, "B")){
            contentValues.put(CoL_7,Res);
            contentValues.put(CoL_8,Gen);
            db.update(TABLE_NAME,contentValues, "Construction = ?",new String[] { fake });
            db.close();
        }
    }


    return true;
}

 public boolean insertData (String lat,String lng){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues =new ContentValues();
    contentValues.put(CoL_2,lat);
    contentValues.put(CoL_3,lng);
    contentValues.put(CoL_4,"A");
    contentValues.put(CoL_7,"B");
    long res =db.insert(TABLE_NAME,null,contentValues);
    db.close();
    return res != -1;



}

在主要活动中,此功能正在插入

       private void goToLocationZoom(double lat, double lng, float zoom) {
    LatLng ll = new LatLng(lat, lng);
    CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll, zoom);
    mGoogleMap.moveCamera(update);
}

Marker marker;


public void Tag(View view)  {

    MarkerOptions options = new MarkerOptions()
            .position(new LatLng(lat, lng))
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
    marker = mGoogleMap.addMarker(options);




    boolean isInserted = myDb.insertData(Double.toString(lat), Double.toString(lng));
    if (isInserted == true) {
        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
    } else
    {Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();}
    intent =new Intent (this,Form1Activity.class);
    startActivity(intent);



}

Second Activityyyyyy是heeeeeee .......

     public class Form1Activity extends AppCompatActivity {
private static RadioGroup radioGroup1,radioGroup2;
private static RadioButton Gr1Option,Gr2Option;
private static Button Enter;

DatabaseHelper myDbForm1;
private EditText Ed1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form1);
    Enter();
}
public void Enter(){
    myDbForm1 = new DatabaseHelper(this);
    radioGroup1 = (RadioGroup) findViewById(R.id.RG1);
    radioGroup2 = (RadioGroup) findViewById(R.id.RG2);
    Enter= (Button) findViewById(R.id.enter1);

    Enter.setOnClickListener(new View.OnClickListener() {
        @SuppressLint("WrongViewCast")
        @Override
        public void onClick(View v) {

            int selected1 =radioGroup1.getCheckedRadioButtonId();
            int selected2 =radioGroup2.getCheckedRadioButtonId();
            Gr1Option= (RadioButton) findViewById(selected1);
            Gr2Option= (RadioButton) findViewById(selected2);
            Ed1= (EditText) findViewById(R.id.editText1);
            if(Gr1Option== null || Gr2Option== null || Ed1==null )
            {
                Toast.makeText(Form1Activity.this,"Select one of the options", Toast.LENGTH_SHORT).show();
            }
            else {
                String a=Gr1Option.getText().toString();
                String b=Gr2Option.getText().toString();
                String c=Ed1.getText().toString();

                boolean isInserted = myDbForm1.updateForm1("A",a,b,c);
                if (isInserted) {
                    Toast.makeText(Form1Activity.this, "Data of form1 Inserted", Toast.LENGTH_LONG).show();
                } else
               {Toast.makeText(Form1Activity.this, "Data of form1 not Inserted", Toast.LENGTH_LONG).show();}

                Toast.makeText(Form1Activity.this, Gr1Option.getText().toString(), Toast.LENGTH_LONG).show();
                Toast.makeText(Form1Activity.this, Gr2Option.getText().toString(), Toast.LENGTH_LONG).show();
                Toast.makeText(Form1Activity.this, Ed1.getText().toString(), Toast.LENGTH_LONG).show();
            }

        }
    });


}

public void Next(View view){
    if(Gr1Option== null || Gr2Option== null|| Ed1==null )
    {
        Toast.makeText(Form1Activity.this,"Select one of the options and press Enter", Toast.LENGTH_SHORT).show();
    }
    else {
        Intent intent =new Intent(Form1Activity.this,Form2Activity.class);
        startActivity(intent);

    }

}

}

第二次更新行的第三个活动.............

  public class Form2Activity extends AppCompatActivity {

private static RadioGroup radioGroup21,radioGroup22;
private static RadioButton Gr21Option,Gr22Option;
private static Button Enter2;

DatabaseHelper myDbForm2;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form2);
    Enter2();
}

public void Enter2(){
    radioGroup21 = (RadioGroup) findViewById(R.id.RG21);
    radioGroup22 = (RadioGroup) findViewById(R.id.RG22);
    Enter2= (Button) findViewById(R.id.enter2);

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

            int selected1 =radioGroup21.getCheckedRadioButtonId();
            int selected2 =radioGroup22.getCheckedRadioButtonId();
            Gr21Option= (RadioButton) findViewById(selected1);
            Gr22Option= (RadioButton) findViewById(selected2);
            if(Gr21Option== null || Gr22Option== null )
            {
                Toast.makeText(Form2Activity.this,"Select one of the options", Toast.LENGTH_LONG).show();
            }
            else
            {

                String a1=Gr21Option.getText().toString();
                String b1=Gr22Option.getText().toString();
                boolean isInserted = myDbForm2.updateForm1("B",a1,b1,"1");
                if (isInserted) {
                    Toast.makeText(Form2Activity.this, "Data of form2 Inserted", Toast.LENGTH_LONG).show();
                } else
                {Toast.makeText(Form2Activity.this, "Data of form2 not Inserted", Toast.LENGTH_LONG).show();}
                Toast.makeText(Form2Activity.this, Gr21Option.getText().toString(), Toast.LENGTH_LONG).show();
                Toast.makeText(Form2Activity.this, Gr22Option.getText().toString(), Toast.LENGTH_LONG).show();
            }

        }
    });


}

2 个答案:

答案 0 :(得分:0)

您尚未在第三个Activity

中初始化“myDbForm2”

答案 1 :(得分:0)

此功能现在适用于我

          public void Enter2(){


 myDbForm2 = new DatabaseHelper(this);
radioGroup21 = (RadioGroup) findViewById(R.id.RG21);
radioGroup22 = (RadioGroup) findViewById(R.id.RG22);
Enter2= (Button) findViewById(R.id.enter2);

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

        int selected1 =radioGroup21.getCheckedRadioButtonId();
        int selected2 =radioGroup22.getCheckedRadioButtonId();
        Gr21Option= (RadioButton) findViewById(selected1);
        Gr22Option= (RadioButton) findViewById(selected2);
        if(Gr21Option== null || Gr22Option== null )
        {
            Toast.makeText(Form2Activity.this,"Select one of the options", Toast.LENGTH_LONG).show();
        }
        else
        {

            String a1=Gr21Option.getText().toString();
            String b1=Gr22Option.getText().toString();
            boolean isInserted = myDbForm2.updateForm1("B",a1,b1,"1");
            if (isInserted) {
                Toast.makeText(Form2Activity.this, "Data of form2 Inserted", Toast.LENGTH_LONG).show();
            } else
            {Toast.makeText(Form2Activity.this, "Data of form2 not Inserted", Toast.LENGTH_LONG).show();}
            Toast.makeText(Form2Activity.this, Gr21Option.getText().toString(), Toast.LENGTH_LONG).show();
            Toast.makeText(Form2Activity.this, Gr22Option.getText().toString(), Toast.LENGTH_LONG).show();
        }

    }
});

}