gpt4 book ai didi

java - 通过Application更新Activity外的TextView

转载 作者:行者123 更新时间:2023-11-29 07:50:04 26 4
gpt4 key购买 nike

我有以下三个类:


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

在我的 list 中:

  • 在应用程序中:android:name="xxx.yyy.zzz.ApplicationView"

    - 在 Activity 中: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 有更好的方法吗?谢谢大家!

最佳答案

我认为当除 Activity 类之外的任何类都知道此 Activity 的布局和/或 View 时,这是一种不好的做法。我的建议是创建在 VideoCapture Activity 中查找和更新 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");

关于java - 通过Application更新Activity外的TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com