Errors with firebaselistadapter



大厅:

public class Lobby extends AppCompatActivity implements View.OnClickListener{
    private TextView text;
    private Button add;
    public FirebaseListAdapter<LobbysName> adapterList;
    final Context context=this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lobby);
        text=(TextView)findViewById(R.id.listtext);
        add=(Button)findViewById(R.id.add_lobby);
        add.setOnClickListener(this);
        readLobby();
    }
    @Override
    public void onClick(View view) {
        //???????? ??? ? ????? dialog_add.xml, ??????? ???????? ??? ??????????? ????:
        LayoutInflater li=LayoutInflater.from(context);
        View dialog=li.inflate(R.layout.dialog_add,null);
        //??????? AlertDialog
        AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(context);
        //??????????? dialog_add.xml ??? ?????? AlertDialog:
        mDialogBuilder.setView(dialog);
        //??????????? ??????????? ???? ??? ????? ?????? ? ???????? ???????:
        final EditText userInput = (EditText) dialog.findViewById(R.id.input_text);
        //??????????? ????????? ? ?????????? ????:
        mDialogBuilder
                .setCancelable(false)
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                //?????? ????? ? ?????????? ? ?????? ????? ?? ???????? ??????:
                                addNewLobby(userInput.getText().toString());
                            }
                        })
                .setNegativeButton("??????",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
        //??????? AlertDialog:
        AlertDialog alertDialog = mDialogBuilder.create();
        //? ?????????? ???:
        alertDialog.show();
    }
    public void addNewLobby(String name){
        FirebaseDatabase.getInstance().getReference().child("lobby").push()
                .setValue(new LobbysName(name,
                        FirebaseAuth.getInstance().getCurrentUser().getEmail()));
        Toast.makeText(getApplicationContext(),"?????? ?????????",Toast.LENGTH_SHORT).show();
    }
    public void readLobby(){
        final ListView list=(ListView)findViewById(R.id.list_lobby);
        /*--------------??????????? ?????? ????? ?????--------------------*/
        list.post(new Runnable() {
            @Override
            public void run() {
                list.setSelection(list.getCount() - 1);
            }
        });
//ERORS THIS-------------------------------------
        adapterList = new FirebaseListAdapter<LobbysName>(this, LobbysName.class, R.layout.itemlobby, FirebaseDatabase.getInstance().getReference().child("lobby")) {
            @Override
            protected void populateView(View v, LobbysName model, int position) {
                text.setText(model.getName());
            }
        };
        list.setAdapter(adapterList);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.settings_item_1:
                finish();
                startActivity(new Intent(getApplicationContext(),Add_User.class));
            case R.id.settings_item_2:
                Toast.makeText(getApplicationContext(),
                        "?????", Toast.LENGTH_LONG).show();
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        finish();
        startActivity(new Intent(getApplicationContext(),Sign_in.class));
    }
}

类lobbysname:

public class LobbysName {
    private String name;
    private String id;

    public LobbysName(String name, String id) {
        this.name = name;
        this.id = id;
    }
    public LobbysName() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

致命异常:

Process: com.example.shcherbuk96.example2firebase, PID: 4434
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
    **at com.example.shcherbuk96.example2firebase.TreeActivity.Lobby$4.populateView(Lobby.java:107)
    at com.example.shcherbuk96.example2firebase.TreeActivity.Lobby$4.populateView(Lobby.java:104)**
    at com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:140)
    at android.widget.AbsListView.obtainView(AbsListView.java:2362)
    at android.widget.ListView.makeAndAddView(ListView.java:1970)
    at android.widget.ListView.fillDown(ListView.java:704)
    at android.widget.ListView.fillFromTop(ListView.java:765)
    at android.widget.ListView.layoutChildren(ListView.java:1716)
    at android.widget.AbsListView.onLayout(AbsListView.java:2161)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5612)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
    at android.view.Choreographer.doCallbacks(Choreographer.java:683)
    at android.view.Choreographer.doFrame(Choreographer.java:619)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

populateView()方法删除此行:

text.setText(model.getName());

替换为:

TextView txt = (TextView) v.findViewById(android.R.id.listtext);
txt.setText((model.getName());

问题在这里:

text=(TextView)findViewById(R.id.listtext);

null,因为您的Activity使用R.layout.activity_lobby,而不是项目布局。

如果要使用项目布局populateView方法中使用:

TextView textItem = (TextView) v.findViewById(R.id.listtext);
textItem.setText((model.getName());

相关内容

  • 没有找到相关文章

最新更新