大厅:
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));
}
}
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)
答案 0 :(得分:0)
从populateView()
方法中删除此行:
text.setText(model.getName());
替换为:
TextView txt = (TextView) v.findViewById(android.R.id.listtext);
txt.setText((model.getName());
答案 1 :(得分:0)
问题在于:
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());