Java 我不能在编辑文本中设置文本
LOGCAT Erorrّّّ java.lang.NullPointerException:尝试调用虚拟方法 空值上的“java.lang.String a.thebook.m_Model.new_book1.getnamebook()” 对象引用 在java类中Java 我不能在编辑文本中设置文本,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,LOGCAT Erorrّّّ java.lang.NullPointerException:尝试调用虚拟方法 空值上的“java.lang.String a.thebook.m_Model.new_book1.getnamebook()” 对象引用 在java类中 package a.thebook; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompat
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数据库获取值。
新书1
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;
}
}
您尚未初始化
模型
变量。
因此,您将得到一个null对象
错误
初始化您的新\u book1
变量,您的代码将正常工作
希望这有帮助:)使用:
private new_book1 model = new new_book1("myBook", "myself");
这将对您有所帮助。模型的初始化在哪里?您的模型有空文本,您没有设置bookname并直接使用getbookname您没有初始化类new\u book1,只是创建了引用,不要忘记投票回答:)即使这样也可能会产生NPE,他应该像
private new_book1 model=new new_book1(“书名”、“作者”)代码>
@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());
}