你能帮我理解为什么当我想在TextView
中设置文字时,我得到了NullPointerException
?我知道,因为我的TextView
为空,但我又如何获得TextView
?
我的逻辑是什么:
nullPoint
并从处理完毕后,将处理结束回MainActivity
并更新TextView
中的文字。这是我的示例代码:
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
}
nullPoint
public class nullPoint {
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
MainActivity ma = new MainActivity();
ma.setTextView(5);
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:layout_gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_gravity="center"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_gravity="center"/>
</LinearLayout>
答案 0 :(得分:0)
我猜nullPoint
课程不在MainActivity
内。因此,在nullPoint
课程内,您没有参考您尝试设置数据的TextView
。
在您的情况下,我建议您实现如下的侦听器。
public interface FirebaseResponseListener {
void onFirebaseResponseReceived(int count);
}
现在从MainActivity
开始,您需要按如下方式实现侦听器。
public class MainActivity extends AppCompatActivity implements FirebaseResponseListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAddNewWord = findViewById(R.id.button);
btnAddNewWord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nullPoint np = new nullPoint();
np.listener = this; // Initialize the listener here
np.takeCount();
}
});
}
public void setTextView(int count){
TextView tv = findViewById(R.id.textView);
tv.setText("count = " + count);
}
@Override
void onFirebaseResponseReceived(int count) {
setTextView(count);
}
}
您还必须在nullPoint
课程中添加一个听众。
public class nullPoint {
public FirebaseResponseListener listener;
//the class gets asynchronous data from the Firebase database and does not know when the process will end
public void takeCount(){
//det data
//.
//.
// finish
//send data to MainActivity and update textView text
listener.onFirebaseResponseReceived(5);
}
}
希望能解决你的问题。