我尝试使用TextViews
数据库填充第二个事件的SQLite
。当用户点击ListView
中的项目时,他/她可以在第二个Activity
上查看活动的详细信息,但每当我点击某个活动时,它就会崩溃。
MainActivity.java
myList.setAdapter(myCursorAdapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor myCursor = (Cursor) myList.getItemAtPosition(position);
String pickedString = myCursor.getString(1);
Intent intent = new Intent(getApplicationContext(),EventDetails.class);
intent.putExtra("name_var", pickedString);
startActivity(intent);
}
}
DBHelper:
public ArrayList<String> getEvent(String name)
{
ArrayList<String> array_list = new ArrayList<String>();
//hp = new HashMap();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery( "select * from events WHERE name='" +name+"'", null );
res.moveToFirst();
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_NAME)));
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_DATE)));
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_DESCRIPTION)));
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_TIME_START)));
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_TIME_END)));
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_ADDRESS)));
array_list.add(res.getString(res.getColumnIndex(EVENTS_COLUMN_POSTCODE)));
return array_list;
}
EventDetails.java
public class EventDetails extends Activity {
private DBHelper mydb;
TextView title = (TextView) findViewById(R.id.event_title);
TextView time_date = (TextView) findViewById(R.id.event_time_date);
TextView description = (TextView) findViewById(R.id.event_description);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String name = intent.getStringExtra("name_var");
mydb = new DBHelper(this);
ArrayList<String> valuesFromDb;
valuesFromDb = mydb.getEvent(name);
title.setText(valuesFromDb.get(1));
time_date.setText(valuesFromDb.get(2));
description.setText(valuesFromDb.get(3));
}
}
答案 0 :(得分:7)
TextView title = (TextView) findViewById(R.id.event_title); TextView time_date = (TextView) findViewById(R.id.event_time_date); TextView description = (TextView) findViewById(R.id.event_description);
您过早地致电findViewById()
并在此处获取NPE,因为该活动在Window
之前不会有onCreate()
。
将这些初始化移至onCreate()
。还要在它们之前添加setContentView()
,在其中设置包含这些ID的视图的布局,以便返回非null值,并且在尝试调用setText()
时不会获得NPE。
答案 1 :(得分:0)
错了: (视图尚未创建)
TextView title = (TextView) findViewById(R.id.event_title);
TextView time_date = (TextView) findViewById(R.id.event_time_date);
TextView description = (TextView) findViewById(R.id.event_description);
正确的方式:
Private TextView title ;
Private TextView time_date ;
Private TextView description ;
然后在setContentView()
之后的OnCreate上title = (TextView) findViewById(R.id.event_title);
time_date = (TextView) findViewById(R.id.event_time_date);
description = (TextView) findViewById(R.id.event_description);
我建议您使用ButterKnife进行视图注入: