この問題を解決するために数時間探し回っていますが、何も見つかりません。行で NullPointerException を取得しています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>
オフライン アクティビティ:
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);
これを解決するための助けをいただければ幸いです