2016-05-14 4 views
-2

У меня есть приложение с tabLayout из 2 фрагментов с listView, и я хочу загрузить контент из URLConnection с Json, но в файле Fragment.java. Geht the Error: can not разрешить метод 'findViewByID (int)'.Ошибка: не удается найти метод символа findviewbyid

public class Fragment1 extends Fragment implements URLConnectionResponse { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     CanteenProvider canProv = new CanteenProvider(); 
     canProv.delegate = this; 
     canProv.getDishes(getActivity().getApplicationContext()); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false); 
    } 

    @Override 
    public void processFinish(List<?> output) { 
     ListView canteenList = (ListView) findViewById(R.id.mensalistView); 
     ArrayList<Dish> canteenItemContent = new ArrayList<Dish>(); 
     canteenItemContent = (ArrayList<Dish>)output; 

     CanteenListAdapter canteenAdapter = new CanteenListAdapter(canteenItemContent, this); 
     canteenList.setAdapter(canteenAdapter); 
    } 


} 

ответ

1

следовать этот ответ на stack overflow

private ListView canteenList=null; 
    private View view = null; 
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     view = inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false); 
     canteenList = (ListView)view.findViewById(R.id.mensalistView); 
     return view; 
    } 

    @Override 
    public void processFinish(List<?> output) { 

     ArrayList<Dish> canteenItemContent = new ArrayList<Dish>(); 
     canteenItemContent = (ArrayList<Dish>)output; 

     CanteenListAdapter canteenAdapter = new CanteenListAdapter(canteenItemContent, view.getContext()); 
     canteenList.setAdapter(canteenAdapter); 
    } 
0

Вы должны создать одну глобальную переменную

View mainContent;

После этого во время накачивания в onCreateView как этот

mainContent = inflater.inflate(R.layout.activity_canteen_tab_mensa, container, false); 
return mainContent; 

и в конце с ведения mainContent Вы можете findViewById вот так

ListView canteenList = (ListView) mainContent.findViewById(R.id.mensalistView);

1

вы должны использовать findViewById из View класса. так что вы можете создать глобальную переменную для ListView:

ListView canteenList; 

затем в вашем onViewCreated инициализации canteenList

@Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     canteenList = view.findViewById(R.id.mensalistView); 
    } 
Смежные вопросы