user2027811
user2027811

Reputation: 4931

Activity is null

my program flow is

  1. activity starts Service.
  2. the service starts TimerTask to get html
  3. when TimerTask get new html, sendBroadcast();
  4. Broadcast class receive "String html"
  5. onReceive is called and starts change(html) method in Activity.
  6. change(String html) method put html to textview in activity.

my problem is at 5 : "Activity instance is null"

my code is

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

and service code is

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

then go to broadcast class. public class Receiver extends 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);
    }

that's all.

why"MainActivity.Iinstance.new AA();"is null?

my guess why activity getting null is

  1. Because webview isn't setcontentview. so now, webview isnt seen, working background. its good for me. but. invisible webview cover all UI and activity UI is threw away.
  2. Because Handler isnt used correctly. invisible webview uses Handler?

after all, i couldnt solved this however i tried many solution.

please help me.

thanks.

Upvotes: 1

Views: 957

Answers (1)

nicopico
nicopico

Reputation: 3636

Activity / Service communication

You cannot keep a static variable of your activity, that would lead to leaks and bugs!

Your activity will be re-created and removed by the Android system depending on various conditions (needing the memory resources, configuration change, etc.)

You should implement communication between your Activity and your Service using Intent.

Set you activity launchMode to singleTop if you don't want to create a new activity each time. You would then need to handle the intent in the onNewIntent(Intent) method of your activity

Loading url from a Service

You cannot use a webView inside a service. You have to use a class like HttpUrlConnection. See Connect and Download Data for more information

Upvotes: 1

Related Questions