0

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

10
  • findViewById(R.id.list); will return null if not found. Do you have that resource? Commented Sep 4, 2013 at 17:16
  • post the xml where you ahve listview Commented Sep 4, 2013 at 17:19
  • @Ahmad My 59th line is : listView.setAdapter(adapter); Commented Sep 4, 2013 at 17:19
  • Yes! I do. I have also tried changing id of my listview, but nothing happened. : @Sotirios Delimanolis Commented Sep 4, 2013 at 17:19
  • @ashu does your class extend listactivity? Commented Sep 4, 2013 at 17:20

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Nothing happened even after removing if else statement & setContentView(R.layout.empty). Still getting same exception in same line.
So do you now have only one setContentView() with the param of a layout file that contains a ListView with id of list?
Okay! Problem solved. Thanks. While I was writing code to read files, to test it, i set content view to empty and i forgot to remove it from there, thereby creating errors.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.