LOGCAT Erorr
java.lang.NullPointerException:尝试调用虚方法 'null上的java.lang.String a.thebook.m_Model.new_book1.getnamebook()' 对象参考
in java Class
package a.thebook;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import a.thebook.m_Model.new_book1;
....
public class my_book extends AppCompatActivity {
private new_book1 model;
private EditText bookname1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_book_activity);
EditText bookname1 = (EditText) findViewById(R.id.editTextbookname2);
bookname1.setText(model.getbookname());
...
}
从firebase数据库获取值。 new_book1
package a.thebook.m_Model;
import java.util.Objects;
public class new_book1 {
public String author;
public String bookname;
public new_book1() {
}
public new_book1(String bookname ,String author) {
this.bookname = bookname;
this.author = author;
}
public String getauthor(int author) {
return this.author;
}
public String getbookname() {
return this.bookname;
}
}
答案 0 :(得分:3)
您尚未初始化model
变量。
因此,您收到null object
错误。
初始化您的new_book1
变量,您的代码将正常工作
希望这会有所帮助:)
答案 1 :(得分:1)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_book_activity);
model = new new_book1("abc","xyz");
EditText bookname1 = (EditText) findViewById(R.id.editTextbookname2);
bookname1.setText(model.getbookname());
}
这会对你有帮助。
答案 2 :(得分:0)
使用:
private new_book1 model = new new_book1("myBook", "myself");