尝试从其他类更改textView中的文本时出现NullPointerException

时间:2018-05-27 17:06:29

标签: java android nullpointerexception

你能帮我理解为什么当我想在TextView中设置文字时,我得到了NullPointerException?我知道,因为我的TextView为空,但我又如何获得TextView

我的逻辑是什么:

  1. 启动申请
  2. 点击按钮
  3. 转到nullPoint并从处理完毕后,将处理结束回MainActivity并更新TextView中的文字。
  4. 这是我的示例代码:

    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>
    

1 个答案:

答案 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);
    }
}

希望能解决你的问题。