Android应用程序更新后,Android应用程序无法运行

时间:2017-06-23 16:04:41

标签: android android-studio sqlite

我最近将Android Studio升级到2.3.3,从那时起我就遇到过这个问题。如果我要部署使用SQLite数据库的应用程序,则该应用程序不起作用。它立即显示应用程序已停止工作的错误。使用SQLite数据库在更新之前创建的所有应用程序都在工作,但是我现在创建的任何新应用程序都没有。我不明白为什么以前工作的代码不再有效。有没有办法让这项工作?

我已使用数据库附加了示例代码。

MainActivity

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
public static ArrayList<String> Array_Names= new ArrayList<String>();
MyDBHandler db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db.getAllContacts();

    db=new MyDBHandler(this,null,null,1);
    db.addContact(new Contact("Andrew", "11111","aj2@gmail.com","Delhi"));
    db.addContact(new Contact("Karl", "22222","ks96@yahoo.com","Bangalore"));
    db.addContact(new Contact("Mike", "33333","mn@gmail.com","Chicago"));
    db.addContact(new Contact("Nathan", "12345","nj@yahoo.com","London"));

    ListAdapter myAdapter = new CustomAdapter(this, Array_Names);
    ListView mylist = (ListView) findViewById(R.id.mylist);
    mylist.setAdapter(myAdapter);

    mylist.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            }

    );

}
}

MyDBHandler

import android.content.ContentValues;
import android.database.sqlite.*;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class MyDBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME= "New_App.db";
public static final String TABLE_NAME= "Contacts";
public static final String TABLE_NAME1= "ContactsSorted";
public static final String COL_ID= "ID";
public static final String COL_NAME= "Name";
public static final String COL_PHNO= "PhoneNumber";
public static final String COL_EMAIL= "EMail";
public static final String COL_ADDRESS= "Address";

public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String Create_query= " CREATE TABLE " + TABLE_NAME + " ( " +
            COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COL_NAME + " TEXT, " +
            COL_PHNO + " TEXT, " +
            COL_EMAIL + " TEXT, " +
            COL_ADDRESS + " TEXT " +
            " ); " ;
    db.execSQL(Create_query);

}

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

public void addContact(Contact contact){

    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues values=new ContentValues();
    values.put(COL_NAME,contact.get_name());
    values.put(COL_PHNO, contact.get_phno());
    values.put(COL_EMAIL,contact.get_email());
    values.put(COL_ADDRESS,contact.get_address());

    db.insert(TABLE_NAME,null,values);
    db.close();
}

public List<Contact> getAllContacts(){
    List<Contact> contactList=new ArrayList<Contact>();
    SQLiteDatabase db= this.getWritableDatabase();
    String Select_query= " SELECT * FROM " + TABLE_NAME1;
    Cursor cursor= db.rawQuery(Select_query,null);

    if(cursor.moveToFirst()){
        do {
            Contact contact= new Contact();
            contact.set_id(Integer.parseInt(cursor.getString(0)));
            contact.set_name(cursor.getString(1));
            contact.set_phno(cursor.getString(2));
            contact.set_email(cursor.getString(3));
            contact.set_address(cursor.getString(4));

            String name=cursor.getString(1) + "\n";
            MainActivity.Array_Names.add(name);
            contactList.add(contact);
        }while (cursor.moveToNext());
    }

    return contactList;
}
}

联系

public class Contact {

int _id;
String _name;
String _phno;
String _email;
String _address;

public Contact(){

}


public Contact(String _address, String _email, int _id, String _name, String _phno) {
    this._address = _address;
    this._email = _email;
    this._id = _id;
    this._name = _name;
    this._phno = _phno;
}

public Contact(String _name,String _phno, String _email,String _address) {
    this._address = _address;
    this._email = _email;
    this._name = _name;
    this._phno = _phno;
}

public int get_id() {
    return _id;
}

public void set_id(int _id) {
    this._id = _id;
}

public String get_name() {
    return _name;
}

public void set_name(String _name) {
    this._name = _name;
}

public String get_phno() {
    return _phno;
}

public void set_phno(String _phno) {
    this._phno = _phno;
}

public String get_address() {
    return _address;
}

public void set_address(String _address) {
    this._address = _address;
}

public String get_email() {
    return _email;
}

public void set_email(String _email) {
    this._email = _email;
}
}

CustomAdapter

public class CustomAdapter extends ArrayAdapter<String> {

public CustomAdapter(@NonNull Context context, ArrayList<String> names) {
    super(context,R.layout.custom_row, names);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater myInflater= LayoutInflater.from(getContext());

    LayoutInflater myinflater= LayoutInflater.from(getContext());
    View customView= convertView;
    if(customView==null){
        customView=myinflater.inflate(R.layout.custom_row,parent,false);

    }
    String singleItem=getItem(position);
    TextView mytext=(TextView) customView.findViewById(R.id.mytext);
    ImageView defimage=(ImageView) customView.findViewById(R.id.myimage);
    mytext.setTextColor(Color.BLACK);
    mytext.setText(singleItem);
    defimage.setImageResource(R.mipmap.contacts_default);
    return customView;


}
}

0 个答案:

没有答案