这是主要活动
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
DB db;
Button addmed,addpl;
TextView PPLNAMERES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DB(this);
ArrayList<String> my_array= new ArrayList<String>();
my_array = db.getTableValues();
Spinner spinner = (Spinner)findViewById(R.id.spin1);
ArrayAdapter<String> my_adapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,my_array);
spinner.setAdapter(my_adapter);
spinner.setOnItemSelectedListener(this);
PPLNAMERES = (TextView)findViewById(R.id.PPLAGETXTVIEW);
}
public void addmedView(View view){
Intent ADDMEDVIEW = new Intent(this,ADDMEDCINEVIEW.class);
startActivity(ADDMEDVIEW);
}
public void addpplview(View view){
Intent ADDPPLVIEWS = new Intent(this,ADDPPLVIEW.class);
startActivity(ADDPPLVIEWS);
}
public void listView(int index){
int getPPLNAME = index;
Cursor res= db.getMainData();
if(res.moveToFirst()){
PPLNAMERES.setText(res.getString(getPPLNAME));
} else{
Toast.makeText(MainActivity.this,"No Data",Toast.LENGTH_LONG).show();
return;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
listView(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
sqliteopenhelper类
public class DB extends SQLiteOpenHelper {
public final static String DBNAME = "MEDCINEDB.db";
public final static String Table_name = "MEDCINETable";
public final static String Table_name2 = "PPLTABLE";
public final static String col1 = "MEDCINEID";
public final static String col2 = "MEDCINENAME";
public final static String col3 = "MEDCINEPURPOSE";
public final static String col4 = "NOTAPLET";
public final static String col1T2 = "ID";
public final static String col2T2 = "NAMEPPL";
public final static String col3T2 = "AGEPPL";
public final static int DBVersion = 2;
public DB(Context context) {
super(context, DBNAME, null, DBVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + Table_name + "(MEDCINEID INTEGER PRIMARY KEY AUTOINCREMENT,MEDCINENAME TEXT,MEDCINEPURPOSE TEXT,NOTAPLET INTEGER)");
db.execSQL("CREATE TABLE " + Table_name2 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAMEPPL TEXT,AGEPPL INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP IF EXISTS" + Table_name);
db.execSQL("DROP IF EXISTS" + Table_name2);
onCreate(db);
}
public boolean inserData(String name, String purpose, String notaplet) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col2, name);
contentValues.put(col3, purpose);
contentValues.put(col4, notaplet);
long Result = db.insert(Table_name, null, contentValues);
if (Result == -1) {
return false;
} else {
return true;
}
}
public boolean insertTb2Data(String Name, String Age) {
SQLiteDatabase db = getWritableDatabase();
ContentValues PPLcontentValues = new ContentValues();
PPLcontentValues.put(col2T2, Name);
PPLcontentValues.put(col3T2, Age);
Long Result = db.insert(Table_name2, null, PPLcontentValues);
if (Result == -1) {
return false;
} else
return true;
}
public Cursor getMainData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select NAMEPPL from " + Table_name2, null);
return result;
}
public ArrayList<String> getTableValues() {
ArrayList<String> my_array = new ArrayList<String>();
try {
SQLiteDatabase db = this.getWritableDatabase();
Cursor resultPPNAME = db.rawQuery("select NAMEPPL from " + Table_name2, null);
resultPPNAME.moveToFirst();
while (resultPPNAME.isAfterLast() == false) {
String PPLNAMESPIN = resultPPNAME.getString(0);
resultPPNAME.moveToNext();
my_array.add(PPLNAMESPIN);
}
} catch (Exception e) {
}
return my_array;
}
}
logcat中的告诉我这条消息
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'
答案 0 :(得分:0)
在PPLNAMERES
中,TextView希望显示Spinner中的选定文本,然后将listView
方法更改为:
public void listView(int index){
PPLNAMERES.setText(my_array.get(index));
}
同样在create方法上声明my_array
与声明的PPLNAMERES
和其他变量相同的方式。