我不确定如何在Logcat输出中找到行号。此外,使用最新的Android Studio版本,常规logcat无法正常工作,因此我必须启动Android设备监视器才能查看它。我只能抓住一个png,真诚地为此道歉。
这是MainActivity类:
public class MainActivity extends Activity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listViewCategories);
String[] categories = getResources().getStringArray(R.array.categories_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, categories);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
/*Toast.makeText(getApplicationContext(),
"Position: " + itemPosition + " List Item: " + itemValue,
Toast.LENGTH_LONG)
.show();*/
Intent intent = new Intent(MainActivity.this, DisplayResult.class);
intent.putExtra("ITEMVALUE", itemValue);
startActivity(intent);
}
});
}
}
这是第二项活动,DisplayResult:
public class DisplayResult extends Activity {
String selectedCategory;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verses);
Bundle b = getIntent().getExtras();
String selectedCategory = b.getString("ITEMVALUE");
//Toast.makeText(DisplayResult.this, "Category passed: " + selectedCategory,
// Toast.LENGTH_LONG).show();
final ListView lv = (ListView) findViewById(R.id.listViewItems);
ArrayList<Promise> results = null;
lv.setAdapter(new MyCustomArrayAdapter(this, results));
} // end onCreate
public ArrayList<Promise> GetPromises() {
String table = "promises";
ArrayList<Promise> promises = new ArrayList<Promise>();
Promise prms = new Promise();
SQLiteDatabase newDB;
//SQLiteDatabase db = MyDBHandler.getReadableDatabase();
//ArrayList<Promise> results = null;
ArrayList<Promise> results = new ArrayList<Promise>();
try {
MyDBHandler dbHandler = new MyDBHandler(this.getApplicationContext());
newDB = dbHandler.getReadableDatabase();
Cursor c = newDB.rawQuery("SELECT * FROM " + MyDBHandler.TABLE_NAME +
" WHERE KEY_CATEGORY = ?", new String[]{selectedCategory});
if (c != null) {
if (c.moveToFirst()) {
do {
String category = c.getString(c.getColumnIndex("KEY_CATEGORY"));
String book = c.getString(c.getColumnIndex("KEY_BOOK"));
String chapter = c.getString(c.getColumnIndex("KEY_CHAPTER"));
String verse = c.getString(c.getColumnIndex("KEY_VERSE"));
String word = c.getString(c.getColumnIndex("KEY_WORD"));
Promise promise = new Promise();
/*promise.add(Strings)
results.add(promise)*/
results.add(new Promise(category, book, chapter, verse, word));
} while (c.moveToNext());
} // end inner if
} // end outer if
} catch (SQLiteException se) {
Log.e(getClass().getSimpleName(), "Could not create or open the database");
} finally {
}
return results;
}
}
这是DBHandler类:
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "promisesdatabase.sqlite";
public static final String TABLE_NAME = "promises";
public static final String KEY_ROWID = "id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_BOOK = "book";
public static final String KEY_CHAPTER = "chapter";
public static final String KEY_VERSE = "verse";
public static final String KEY_WORD = "word";
public MyDBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String DATABASE_CREATE =
"CREATE TABLE if not exists " + TABLE_NAME + " (" +
KEY_ROWID + "integer PRIMARY KEY," +
KEY_CATEGORY + " TEXT," +
KEY_BOOK + " TEXT," +
KEY_CHAPTER + " TEXT," +
KEY_VERSE + " TEXT," +
KEY_WORD + " TEXT" + ")";
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
这是CustomArrayAdapter类:
public class MyCustomArrayAdapter extends ArrayAdapter {
private static ArrayList<Promise> promiseArrayList;
private LayoutInflater mInflater;
public MyCustomArrayAdapter(Context context, ArrayList<Promise> results) {
super(context, 0, results);
promiseArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return promiseArrayList.size();
}
public Object getItem(int position) {
return promiseArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
// Promise Promise = getItem(position); took out due to other blog geekswithblogs
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder holder; // view lookup cache stored in tag
if (convertView == null) {
convertView = mInflater.inflate(R.layout.verses, null);
holder = new ViewHolder();
//LayoutInflater inflater = LayoutInflater.from(getContext());
//convertView = inflater.inflate(R.layout.verses, parent, false);
holder.txtcategory = (TextView) convertView.findViewById(R.id.txtViewCategory);
holder.txtbook = (TextView) convertView.findViewById(R.id.txtViewBook);
holder.txtchapter = (TextView) convertView.findViewById(R.id.txtViewChapter);
holder.txtverse = (TextView) convertView.findViewById(R.id.txtViewVerse);
holder.txtword = (TextView) convertView.findViewById(R.id.txtViewWord);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
holder.txtcategory.setText(promiseArrayList.get(position).getCategory());
holder.txtbook.setText(promiseArrayList.get(position).getBook());
holder.txtchapter.setText(promiseArrayList.get(position).getChapter());
holder.txtverse.setText(promiseArrayList.get(position).getVerse());
holder.txtword.setText(promiseArrayList.get(position).getWord());
// Return the completed view to render on screen
return convertView;
}
private static class ViewHolder {
TextView id;
TextView txtcategory;
TextView txtbook;
TextView txtchapter;
TextView txtverse;
TextView txtword;
}
还有一个Promise类(数据模型),但我认为没必要发布。
这里是logcat png
答案 0 :(得分:0)
我可能错了,但该行
ArrayList<Promise> results = null;
未阅读
ArrayList<Promise> results = GetPromises();
问题似乎是您的DisplayResult中的ListView正在尝试使用MyCustomAdapter中的数据进行显示,该数据被赋予null而不是Promises的有效ArrayList。