简体   繁体   中英

NullPointer Exception while setting custom listview adapter

I have been searching around for few hours to solve this problem but i can't find anything. I am getting a NullPointerException in line 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); 
}

 }

This is the code for CustomListViewAdapter class :

 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;
}
  }

Here's the code for RowItem class:

 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;
}
 }

This is my 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

My 59th line is : listView.setAdapter(adapter);

Any help to solve this would be appreciated

Correct me if I'm wrong but it looks like you are calling

setContentView(R.layout.empty);

again just before you try to initialize listView . Assuming R.layout.empty doesn't contain a ListView with id of list it will return null when you try to initialize listView .

For reasons like this, it is rarely a good idea to call setContentView() more than once in the same Activity . There is almost always a better way, IMHO.

like codeMagic say, either your layout "empty" or "offline" doesn´t contain the listView

why not use only one layout, that will contain the listview if you don´t want to show just set

listView.setVisibility(View.GONE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM