尝试向firebase服务器

时间:2017-11-28 18:47:37

标签: java android firebase firebase-realtime-database

您能否知道此代码有什么问题?

public class NewMessageActivity extends AppCompatActivity {

private DatabaseReference mDatabaseReference;
String mDisplayName = "John";
EditText mNewMessageField;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final EditText mNewMessageField = (EditText) findViewById(R.id.newMessageText);
    setContentView(R.layout.activity_new_message);
    ImageButton mSendButton = (ImageButton) findViewById(R.id.sendButton);
    mSendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            sendMessage();
            finish();
        }
    });
}

private void sendMessage(){
    mDatabaseReference= FirebaseDatabase.getInstance().getReference();
    String input = mNewMessageField.getText().toString();
    SingleMessage singleMessage = new SingleMessage(input, mDisplayName);
    mDatabaseReference.child("messages").push().setValue(singleMessage);
}

}

在我按下发送按钮后,应用程序立即停止工作,我收到此错误消息:

  

致命的例外:主要                                                                            显示java.lang.NullPointerException                                                                                在com。 .NewMessageActivity.sendMessage(NewMessageActivity.java:42)                                                                                在com。 .NewMessageActivity.access $ 000(NewMessageActivity.java:14)                                                                                at com。***。NewMessageActivity $ 1.onClick(NewMessageActivity.java:33)                                                                                在android.view.View.performClick(View.java:4209)                                                                                在android.view.View $ PerformClick.run(View.java:17457)                                                                                在android.os.Handler.handleCallback(Handler.java:725)                                                                                在android.os.Handler.dispatchMessage(Handler.java:92)                                                                                在android.os.Looper.loop(Looper.java:153)                                                                                在android.app.ActivityThread.main(ActivityThread.java:5341)                                                                                at java.lang.reflect.Method.invokeNative(Native Method)                                                                                在java.lang.reflect.Method.invoke(Method.java:511)                                                                                在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:929)                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)                                                                                at dalvik.system.NativeStart.main(Native Method)   11-28 20:05:51.566 508-527 /? E / AppErrorDialog:无法获得ILowStorageHandle实例

1 个答案:

答案 0 :(得分:1)

您需要先致电setContentView(才能搜索EditText窗口小部件。您还必须使用在包级别声明的mNewMessageField实例。不要在onCreate()中声明新实例。

setContentView(R.layout.activity_new_message);
mNewMessageField = (EditText) findViewById(R.id.newMessageText);  // <= CHANGED
if (mNewMessageField == null) {
    System.out.println("mNewMessageField is NULL");
}

要获得更多线索,请添加此调试输出:

private void sendMessage(){
    mDatabaseReference= FirebaseDatabase.getInstance().getReference();
    if (mNewMessageField == null) {
        System.out.println("mNewMessageField is NULL");
    }
    Editable ed = mNewMessageField.getText();

    if (ed == null) {
        System.out.println("Editable is NULL");
    }
    String input = mNewMessageField.getText().toString();
    System.out.println("input=" + input);

    SingleMessage singleMessage = new SingleMessage(input, mDisplayName);
    mDatabaseReference.child("messages").push().setValue(singleMessage);
}