【问题标题】:Activity is null活动为空
【发布时间】:2013-02-11 16:07:06
【问题描述】:

我的程序流程是

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

我的问题在 5 处:“活动实例为空”

我的代码是

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

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

    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();”为空?

我猜为什么活动为空

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

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

请帮助我。

谢谢。

【问题讨论】:

  • 1.不可读的问题格式 2. 不可读的代码格式 3. 你忘记了堆栈跟踪。

标签: android nullpointerexception


【解决方案1】:

活动/服务通信

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

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

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

如果您不想每次都创建新活动,请将活动 launchMode 设置为 singleTop。然后,您需要在活动的 onNewIntent(Intent) 方法中处理意图

从服务加载 url

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

【讨论】:

    猜你喜欢
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多