从Fragment更新活动中的textview

时间:2013-04-28 20:01:50

标签: android android-activity textview

如何从TextView

更新活动A中的DialogFragment

在我的情况下,点击DialogFragment中的关闭按钮后,我需要调用可以更改TextView的方法

这是来自Activity的方法:

public void setCountText(String text){
    CountItem = (TextView) findViewById(R.id.tv_numOfItem);
    CountItem.setText(text);
}

DialogFragment中的Onclick按钮:

View.OnClickListener closeHandler = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        new menufeedActivity().setQountText(getCount);


        InfoOrderDialog.this.dismiss();

    };

};

任何人都知道如何解决这个问题?

04-29 03:08:17.513: E/AndroidRuntime(1738): FATAL EXCEPTION: main
04-29 03:08:17.513: E/AndroidRuntime(1738): java.lang.NullPointerException
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.app.Activity.findViewById(Activity.java:1744)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.example.android.rssfeed.menufeedActivity.setQountText(menufeedActivity.java:169)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.example.android.rssfeed.InfoOrderDialog$2.onClick(InfoOrderDialog.java:110)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.view.View.performClick(View.java:3110)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.view.View$PerformClick.run(View.java:11928)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.os.Handler.handleCallback(Handler.java:587)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.os.Looper.loop(Looper.java:132)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at android.app.ActivityThread.main(ActivityThread.java:4025)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at java.lang.reflect.Method.invoke(Method.java:491)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
04-29 03:08:17.513: E/AndroidRuntime(1738):     at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:17)

您可以这样称呼:

替换:

 new menufeedActivity().setQountText(getCount);

使用:

((menufeedActivity) getActivity()).setCountText(getCount);

答案 1 :(得分:8)

就这样做。

TextView textview = (TextView)getActivity().findViewById(R.id.yourtextview);
textview.setText("text");

答案 2 :(得分:0)

在你的片段中:调用getActivity()。startActivityForResult - 完成之前的方法,并在托管片段的活动中捕获它:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

//your method to update textview
setCountText();

}

编辑:我找到了一个更好的解决方案。检查一下:https://stackoverflow.com/a/15121529/2245646

答案 3 :(得分:0)

在科特林

val textview = activity!!.findViewById<View>(R.id.textView) as TextView
        textview.text = "your_text"