我有一个ListView
的活动,我一直在尝试使用字符串ArrayList
中的数据填充列表视图这是活动代码
package com.example.arun.db;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class List_view extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> throwlist = new ArrayList<String>();
ListView Trainlist;
Trainlist=(ListView)findViewById(R.id.station_list);
SQLiteDatabase db = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
setContentView(R.layout.activity_list_view);
try{
Cursor data_fetch = db.rawQuery("Select Station_name From Station", null);
String[] station_array = new String[data_fetch.getCount()];
int i= 0;
while (data_fetch.moveToNext()) {
String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name"));
station_array[i] = name;
Log.d("STATION_GET","Retrieved station " + station_array[i]);
//Toast.makeText(List_view.this, "Retrieved station " + station_array[i] , Toast.LENGTH_SHORT).show();
throwlist.add(name);
i++;
}
data_fetch.close();
}catch(Exception e)
{
Toast.makeText(List_view.this, "An Error occured Retrieving your data" , Toast.LENGTH_LONG).show();
}
try {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, throwlist);
Trainlist.setAdapter(arrayAdapter);
}catch (Exception e){
Log.d("Error in listview ",e.toString());
Toast.makeText(List_view.this, "Listview Error" , Toast.LENGTH_LONG).show();
}
}
}
我已经确定了该部分 `
try {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, throwlist);
Trainlist.setAdapter(arrayAdapter);
}catch (Exception e){
Log.d("Error in listview ",e.toString());
Toast.makeText(List_view.this, "Listview Error" , Toast.LENGTH_LONG).show();
}`
抛出错误
10-28 18:03:42.004 11549-11549/com.example.arun.db D/Error in listview: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
在进一步调查此错误后,我知道这个错误可能是由于here
中没有在xml文件中为特定活动声明的列表视图引起的但是ListView在xml中有明确的定义 下面是xml代码:
<ListView
android:id="@+id/station_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="1.0" />
关于为什么会这样做的任何想法
答案 0 :(得分:1)
你必须先设置setContentView(R.layout.activity_list_view);
而不是像下面的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
ArrayList<String> throwlist = new ArrayList<String>();
ListView Trainlist;
Trainlist=(ListView)findViewById(R.id.station_list);
SQLiteDatabase db = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
// other code
}