我收到以下错误:
方法findViewById(int)未定义类型ListViewFragment
ListViewFragment是
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);
//这是我收到错误的地方,此后的行
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) {
}
}
我知道这是一个常见的错误,但我无法理清
答案 0 :(得分:0)
如果viewvdc
属于您可以使用viewvdc = (TextView) getView().findViewById(R.id.viewvdc);
答案 1 :(得分:0)
移动
下面的一行View rootView = inflater.inflate(R.layout.fragment_listview, container, false);
并将其更改为
viewvdc = (TextView) rootView .findViewById(R.id.viewvdc);
如果它在R.layout.fragment_listview