2015-11-06 43 views
0

一旦我午餐應用程序,我就會面臨崩潰,我試圖從微調器中選擇值並將其設置爲文本視圖。它沒有微調工作正常。 現在它根本不工作。 預先感謝您從sqlite中獲取數據並將其設置爲來自微調器的文本視圖

這裏是在logcat中的主要活動

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; 

} 
} 

告訴我這個消息

顯示java.lang.NullPointerException:嘗試在空對象引用上調用虛方法'void android.widget.TextView.setText(java.lang.CharSequence)'

+2

意味着'PPLAGETXTVIEW' TextView在activity_main.xml佈局中不可用? –

+0

工作正常,但是當我嘗試從logcat中的微調器崩潰選擇不同的值時顯示此消息無法從CursorWindow讀取第0行,第1列。在從中訪問數據之前,確保Cursor已正確初始化。 – OWA

回答

0

PPLNAMERES TextView中要顯示所選文本從微調再去做通過改變listView方法:

public void listView(int index){ 
    PPLNAMERES.setText(my_array.get(index)); 
} 

還宣佈my_array外面創建方法相同的方式,宣佈PPLNAMERES和其他變量。

相關問題