我有以下活动:CoffeeActivity
和DrinkActivity
这是我的主要活动,它有一个ListView
public class CalculationHistoryListActivity extends AppCompatActivity {
Save saveData;
ListView listView;
ArrayList arrayListdrink,arrayListCoffee;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation_history_list);
// Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
// Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
//sqlite database
saveData = new Save(CalculationHistoryListActivity.this);
//listview
listView = (ListView) findViewById(R.id.list);
//coffee list
arrayListCoffee = saveData.getAllCoffeeCalculation();
ArrayAdapter coffeeArrayAdapter = new ArrayAdapter(CalculationHistoryListActivity.this,
R.layout.list_view_item_coffee, R.id.total_money_coffee, arrayListCoffee);
listView.setAdapter(coffeeArrayAdapter);
//drink list
arrayListdrink = saveData.getAllDrinkCalculation();
ArrayAdapter drinkArrayAdapter = new ArrayAdapter(CalculationHistoryListActivity.this,
R.layout.list_view_item_coffee, R.id.total_money_drink,arrayList);
listView.setAdapter(drinkArrayAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_calculation_history_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是我的数据库助手
public class Save extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "my.db";
private static final String TABLE = "mytable";
private static final String COFFEE = "coffee";
private static final String DRINK = "drink";
public Save(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query ;
query = "CREATE TABLE " +TABLE+ "(" +COFFEE+ " TEXT, " +DRINK+ " TEXT " + ")";
sqLiteDatabase.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL(" DROP FILE IF EXISTS " + TABLE);
onCreate(sqLiteDatabase);
}
//this is coffee section
public boolean addDatabaseToCoffee(int i){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COFFEE,i);
long chaker = sqLiteDatabase.insert(TABLE,null,contentValues);
if (chaker == -1){
return false;
}else {
return true;
}
}
//this is drink section
public boolean addDatabaseToDrink(int i){
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DRINK,i);
long chaker = sqLiteDatabase.insert(TABLE,null,contentValues);
if (chaker == -1){
return false;
}else {
return true;
}
}
//coffee
public ArrayList<String> getAllCoffeeCalculation() {
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE,null);
res.moveToFirst();
while(res.isAfterLast() == false){
array_list.add(res.getString(res.getColumnIndex(COFFEE)));
res.moveToNext();
}
return array_list;
}
//drink
public ArrayList<String> getAllDrinkCalculation() {
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE,null);
res.moveToFirst();
while(res.isAfterLast() == false){
//array_list.add(res.getString(res.getColumnIndex(DRINK)));
res.moveToNext();
}
return array_list;
}
}
我的问题是我有两个活动,当我一次性使用咖啡或饮料活动时它没关系,但如果我想同时使用两个活动,我的应用程序崩溃
我的LogCat
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at android.widget.AbsListView.obtainView(AbsListView.java:3065)
at android.widget.ListView.makeAndAddView(ListView.java:1945)
at android.widget.ListView.fillDown(ListView.java:719)
at android.widget.ListView.fillFromTop(ListView.java:785)
at android.widget.ListView.layoutChildren(ListView.java:1735)
at android.widget.AbsListView.onLayout(AbsListView.java:2852)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.widget.ScrollView.onLayout(ScrollView.java:2537)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:344)
at android.widget.FrameLayout.onLayout(FrameLayout.java:281)
at android.view.View.layout(View.java:17995)
at android.view.ViewGroup.layout(ViewGroup.java:5817)
答案 0 :(得分:2)
您的错误来自内部适配器。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
--> at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
--> at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
--> at android.widget.AbsListView.obtainView(AbsListView.java:3065)
首先,你的饮品清单是空的......你评论了这一行
//array_list.add(res.getString(res.getColumnIndex(DRINK)));
这里的arrayList
是什么?
arrayListdrink = saveData.getAllDrinkCalculation();
ArrayAdapter drinkArrayAdapter = new ArrayAdapter(CalculationHistoryListActivity.this,
R.layout.list_view_item_coffee, R.id.total_money_drink,arrayList);
^^^
你应该DROP TABLE
,而不是FILE
......
sqLiteDatabase.execSQL(" DROP FILE IF EXISTS " + TABLE);
如果您的数据库有效,那么我发现您的数据没有其他错误。清除您的应用数据以重新创建数据库,修复这些问题,然后您应该查看饮料值列表。
无论如何,没有你不能在一个ListView中显示两个适配器。
但是,您可以将数据库的两列添加到一个ArrayList中,并在一个适配器中显示。
// coffee and drink
public ArrayList<String> getAllCoffeeCalculation() {
ArrayList<String> array_list = new ArrayList<String>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE,null);
res.moveToFirst();
while(res.isAfterLast() == false){
String c = res.getString(res.getColumnIndex(COFFEE));
String d = res.getString(res.getColumnIndex(DRINK));
array_list.add(c + "\t" + d);
res.moveToNext();
}
return array_list;
}
然后,在活动中。
saveData = new Save(CalculationHistoryListActivity.this);
listView = (ListView) findViewById(R.id.list);
arrayListCoffee = saveData.getAllCoffeeCalculation();
ArrayAdapter<String> coffeeArrayAdapter = new ArrayAdapter<String>(
CalculationHistoryListActivity.this,
android.R.layout.simple_list_item_1,
arrayListCoffee);
listView.setAdapter(coffeeArrayAdapter);