i'am facing a crash once i lunch the app, i'am trying to select value from a spinner and set it in text view. it was working fine without the spinner. now its not working at all. thank you in advance
Here is the Main activity
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) {
}
}
the sqliteopenhelper class
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;
}
}
in the logcat show me this message
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
Aucun commentaire:
Enregistrer un commentaire