我一直在寻找几个小时来解决这个问题,但我找不到任何东西。我在行listView.setAdapter(adapter);
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
OfflineActivity:
public class OfflineActivity extends Activity implements OnItemClickListener
{
String[] fileName;
String[] name;
List<RowItem> rowItems;
String[] dis;
ListView listView;
BufferedReader br;
InputStream in;
String line;
File file;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
File f = new File(Environment.getExternalStorageDirectory()+"/Android/data/com.example.test/database/");
f.mkdirs();
fileName = f.list();
String[] nameTemp = new String[fileName.length + 1];
String[] disTemp = new String[fileName.length + 1];
if(fileName[0] == null){
setContentView(R.layout.empty);
}
else{
setContentView(R.layout.offline);
try{
for(int i=0; i<fileName.length; i++){
file = new File(Environment.getExternalStorageDirectory().toString()+"/Android/data/com.example.test/database/"+fileName[i]);
in = new FileInputStream(file);
br = new BufferedReader(new InputStreamReader(in));
nameTemp[i] = br.readLine();
line = br.readLine();
disTemp[i] = br.readLine();
}
}catch(FileNotFoundException e){} catch(IOException e){}
name = nameTemp;
dis = disTemp;
setContentView(R.layout.empty);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < name.length; i++) {
if(name[i]==null) break;
RowItem item = new RowItem(name[i], dis[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Loading Lyrics for "+name[position]+"...", Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, ShowDataActivity.class);
i.putExtra("name", name[position]);
i.putExtra("fileName", fileName[position]);
startActivity(i);
}
}
这是CustomListViewAdapter类的代码:
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
public CustomListViewAdapter(Context context, int resourceId, List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
return convertView;
}
}
这是RowItem类的代码:
public class RowItem {
private String title;
private String desc;
public RowItem( String title, String desc) {
this.title = title;
this.desc = desc;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + desc;
}
}
这是我的logcat:
E/AndroidRuntime(20534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.OfflineActivity}: java.lang.NullPointerException
E/AndroidRuntime(20534): at com.example.test.OfflineActivity.onCreate(OfflineActivity.java:59)
W/ActivityManager(22626): Force finishing activity com.example.test/.OfflineActivity
我的第59行行是: listView.setAdapter(adapter);
任何帮助解决这个问题都将受到赞赏
答案 0 :(得分:3)
如果我错了,请纠正我,但看起来你正在打电话
setContentView(R.layout.empty);
再次尝试初始化listView
之前。假设R.layout.empty
不包含ListView
id
list
,当您尝试初始化null
时,它将返回listView
。
出于这样的原因,在同一setContentView()
内多次拨打Activity
是一个不错的主意。几乎总有一种更好的方式,恕我直言。
答案 1 :(得分:1)
为什么不只使用一个布局,如果你不想只显示
,它将包含listview listView.setVisibility(View.GONE);