通过Application更新Activity之外的TextView

时间:2014-02-17 10:24:21

标签: java android

我有以下三个类:


public class ApplicationView extends Application {  

    private static Context mContext;
    private static final String    TAG   = "ApplicationView ";  

    public static Context getContext() {
        Log.i(TAG, "getContext: " + mContext); 
        return mContext;
    }

    public static void setContext(Context mmContext) {
        Log.i(TAG, "setContext: " + mmContext);         
        mContext = mmContext;

    }
}

    public class VideoCapture extends Activity implements SurfaceHolder.Callback {

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main); 
            ApplicationView.setContext(this);   
        }
    }

--------------------------
public class OverlayView extends SurfaceView  implements SurfaceHolder.Callback {

    public void updateText()
    {       
        Context mcontext = ApplicationView.getContext();
        ((VideoCapture)mcontext).runOnUiThread(new Runnable(){
                    public void run(){                      
                        String NewText = "bla bla bla"                         
                        TextView txtView = (TextView)findViewById(R.id.txt_status);
                        txtView.setText(NewText);
                    }
                });
    }  
}

在我的清单中:

    应用程序中的
  • :android:name =“xxx.yyy.zzz.ApplicationView”

    - 活动中:android:name =“xxx.yyy.zzz.VideoCapture”


当我向Update Text调用以下NullPointerException错误时,它给了我:

02-17 11:15:27.850: E/AndroidRuntime(15834): FATAL EXCEPTION: main
02-17 11:15:27.850: E/AndroidRuntime(15834): java.lang.NullPointerException
02-17 11:15:27.850: E/AndroidRuntime(15834):    at xx.yyy.zzz.OverlayView$2.run(OverlayView.java:137)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at android.app.Activity.runOnUiThread(Activity.java:4591)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at xxx.yyy.zzz.OverlayView.updateText(OverlayView.java:133)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at xxx.yyy.zzz.OverlayView$1.onPreviewFrame(OverlayView.java:122)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:754)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at android.os.Looper.loop(Looper.java:137)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at android.app.ActivityThread.main(ActivityThread.java:4745)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at java.lang.reflect.Method.invoke(Method.java:511)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-17 11:15:27.850: E/AndroidRuntime(15834):    at dalvik.system.NativeStart.main(Native Method)

我做错了什么? 有一种更好的方法可以在OverlayView中调用updateText吗? 谢谢大家!

2 个答案:

答案 0 :(得分:4)

我认为除了Activity类之外的任何类都知道此Activity的布局和/或视图时,这是不好的做法。 我的建议是创建在VideoCapture活动中查找和更新TextView并在UI线程上执行此操作的方法:

public void updateText(final String text) {       
        runOnUiThread(new Runnable() {
                    public void run(){                                               
                        TextView txtView = (TextView)findViewById(R.id.txt_status);
                        txtView.setText(text);
                    }
                });
    }

并将其称为OverlayView

Context mcontext = ApplicationView.getContext();
((VideoCapture) mcontext).updateText("bla bla bla");

答案 1 :(得分:1)

我认为findViewById()给出了null作为结果,因为它在OverlayView的上下文中执行,但它应该在VideoCapture的上下文中执行,如下所示:

TextView txtView = (TextView) ((VideoCapture)mcontext)findViewById(R.id.txt_status);
txtView.setText(text);