gpt4 book ai didi

android - Activity 为空

转载 作者:行者123 更新时间:2023-11-30 03:47:06 26 4
gpt4 key购买 nike

我的程序流程是

  1. Activity 启动服务。
  2. 服务启动TimerTask获取html
  3. 当 TimerTask 获取新的 html 时,sendBroadcast();
  4. 广播类接收“String html”
  5. 在 Activity 中调用 onReceive 并启动 change(html) 方法。
  6. change(String html) 方法将 html 放入 Activity 中的 textview。

我的问题在 5:“Activity 实例为空”

我的代码是

public class MainActivity extends Activity {
static MainActivity Iinstance;
private String ServiceName = myService.class.getCanonicalName();
ServiceStatus servicestatus = new ServiceStatus();
static Handler handler = new Handler();
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setSoftInputMode(
LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
Iinstance = this;

Button B;
//there is clicklistener for B to start service.
}

class AA{
void A(String html){
tv.setText(html);
}
}

服务代码是

public class myService extends Service {
//fields

public int onStartCommand(Intent i, int flags, int startId) {
//code to start TimerTask.
}

//register the receiver.
CancelReceiver receiver = new CancelReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("MY_ACTION");
registerReceiver(receiver, intentFilter);
return START_REDELIVER_INTENT;
}

class task extends TimerTask{
@Override
public void run() {
try {//get html from webviewClient

handlers.post(new Runnable(){
@Override
public void run() {
//webView load URL.I have two doubts around this.

WebView webView = new WebView(myService.this);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new HogeWebViewClinet());
webView.addJavascriptInterface(new WebViewLogger(), "webViewLogger");
String Url = (String) map.get("URL");
webView.loadUrl(Url);
}
});

}
}

class HogeWebViewClinet extends WebViewClient {
@Override public void onPageFinished(WebView view, String url) {
//throw the html from webview to "class WebViewLogger"as "String str"by loading javascript.
view.loadUrl("javascript:window.webViewLogger.log(document.documentElement.outerHTML);");
}
}

class WebViewLogger {
public void log(String str) {
Intent broadcastIntent = new Intent();
broadcastIntent.putExtra("Status", str);
broadcastIntent.setAction("MY_ACTION");
sendBroadcast(broadcastIntent);
}
}
}

然后去广播课。 公共(public)类接收器扩展 BroadcastReceiver {

    String serviceResult = null;

@Override
public void onReceive(Context arg0, Intent intent) {
AA aa = MainActivity.Iinstance.new AA(); //threw error in this line.
aa.a(intent.getStringExtra(Status);
}

就是这样。

为什么“MainActivity.Iinstance.new AA();”为空?

我猜为什么 activity 为空是

  1. 因为 webview 不是 setcontentview。所以现在,看不到 webview,工作背景。这对我有好处。但。不可见的 webview 覆盖所有 UI 和活​​动 UI 被丢弃。
  2. 因为没有正确使用 Handler。不可见的 webview 使用处理程序?

毕竟,我无法解决这个问题,但是我尝试了很多解决方案。

请帮帮我。

谢谢。

最佳答案

Activity/服务沟通

不能保留 Activity 的静态变量,这会导致泄漏和错误!

Android 系统会根据各种情况(需要内存资源、配置更改等)重新创建和删除您的 Activity

您应该使用 Intent 在 Activity 和 Service 之间实现通信。

设置你的 Activity launchModesingleTop 如果你不想每次都创建一个新的 Activity 。然后您需要处理 onNewIntent(Intent) 中的 Intent 你的 Activity 方式

从服务加载 url

您不能在服务中使用 webView。您必须使用像 HttpUrlConnection 这样的类。参见 Connect and Download Data了解更多信息

关于android - Activity 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14816000/

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