0

Ребята,помогите. Пытаюсь вывести список бесед,но выбивает ошибку.

public class Lobby extends ListFragment implements View.OnClickListener{
    private Button btn;
    private FirebaseListAdapter<String> adapterList;
    private String str;
    private ListView list;
    private ArrayList<String> names;
    private FirebaseUser user;

    public Lobby() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_lobby, container, false);

        user=FirebaseAuth.getInstance().getCurrentUser();
        list = (ListView) v.findViewById(R.id.lobby_list);
        readLobby();
        return v;
    }

    public void readLobby() {
        names=new ArrayList<String>();
        adapterList = new FirebaseListAdapter<String>(getActivity(), String.class, R.layout.lobby_list_item, FirebaseDatabase.getInstance().getReference().child("lobby")) {
            @Override
            protected void populateView(View vg, String model, int position) {
                TextView textView=(TextView)vg.findViewById(R.id.lobby_list_items);
                textView.setText(model);
                names.add(model);
            }
        };

        list.setAdapter(adapterList);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                TextView textV=(TextView)v.findViewById(R.id.lobby_list_items);
                String str=textV.getText().toString();
                for(int i=0;i<names.size();i++){
                    if(str.equals(names.get(i))){
                        Fragment yfc = new Chat();
                        Bundle bundle = new Bundle();
                        bundle.putString("tag", names.get(i));
                        yfc.setArguments(bundle);
                        FragmentManager fragmentManager = getFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.frame,yfc).commit();
                        /*finish();
                        Intent intent=new Intent(getApplicationContext(), NavigationActivity.class);
                        intent.putExtra("name",names.get(i));
                        startActivity(intent);*/
                    }
                }
            }
        });
    }

    public void addNewLobby(String name) {
        FirebaseDatabase.getInstance().getReference().child("lobby").child(name)
                .setValue(name);
    }
}

Вот ошибки:

by.ittech.test_chat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: by.ittech.test_chat, PID: 30551
        java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.support.v4.app.ListFragment.ensureList(ListFragment.java:346)
            at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:147)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
            at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
            at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
            at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
            at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
            at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Подскажите пожалуйста,где мой косяк?)

fragment_lobby.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="by.ittech.test_chat.Lobby">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/lobby_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/lobby_add"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <Button
            android:text="@string/lobby_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:id="@+id/lobby_add"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

</FrameLayout>

lobby_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/lobby_list_items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="30dp"
        android:layout_marginTop="14dp"
        android:layout_centerVertical="true"
        android:gravity="center_horizontal|center_vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

2 ответа 2

2

Вы в делаете list.setAdapter(adapterList) еще до того, как оно проинициализировано в onCreateView();

1
  • теперь уже другая ошибка: Your content must have a ListView whose id attribute is 'android.R.id.list'
    – shcherbuk
    27 мар 2017 в 12:01
1

Ребята,всё заработало. Помогло просто заменить наследование ListFragment на Fragment

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.