The findViewById (int) method is undefined for the ListViewFragment type

advertisements

I am getting the following error:

The method findViewById(int) is undefined for the type ListViewFragment

And the ListViewFragment is

package com.yogeshojha.nagariksahayata;

import java.util.ArrayList;
import java.util.List;

import com.yogeshojha.nagariksahayata.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.app.Activity;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class ListViewFragment extends Fragment implements OnItemSelectedListener{
public ListViewFragment(){}
Spinner mspinner;
TextView viewvdc;//declare spinner globally
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    String [] name = {"नेपाली नागरिकता","नेपाली नागरिकता","नेपाली नागरिकता"};
    System.out.println(name.length);

//This is where I get error, The line after this

    viewvdc = (TextView) findViewById(R.id.viewvdc);
View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
mspinner        = (Spinner) rootView.findViewById(R.id.spinnervdc);
//set adapter for spinner here
adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item,name);
// you can use getActivity() or this 

mspinner.setAdapter(adapter);
return rootView;
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
    long id) {
    mspinner.setSelection(position);
    String selState = (String) mspinner.getSelectedItem();
    viewvdc.setText("Selected Android OS:" + selState);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
}

I know this is a common mistake but i cant sort out


if viewvdc belongs to the activity you can use viewvdc = (TextView) getView().findViewById(R.id.viewvdc);