Android: Calling startActivity() from outside of an Activity context

2.7k views Asked by At

I have a Textview in my alert dialogue box and i am placing a URL in the text? When i use the following piece of code it gives me an error:

                final TextView message = new TextView(context); 
                final SpannableString s = new SpannableString(context.getText(R.string.SEND_SMS));   
                message.setText(s);
                message.setPadding(10, 0, 4, 0);
                message.setGravity(Gravity.AXIS_CLIP);
                message.setMovementMethod(LinkMovementMethod.getInstance());

                new AlertDialog.Builder(MyClass.this)   
                .setTitle("Message 1\nStep 1 of 4")   
                .setView(message)   
                .setPositiveButton(R.string.CONTINUE,new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   

                        Log.e("@@","Inside OnClick");
                        //new CheckLoginStatus().execute();
                        sendSMS();
                        myProgressDialog=new ProgressDialog(MyClass.this);
                        myProgressDialog.setTitle("Message 2\nStep 2 of 4");
                        myProgressDialog.setMessage("Message\n" +
                                "Please wait... attempt "+(count));
                        myProgressDialog.setCancelable(false);
                        myProgressDialog.show();

                        Thread splashTread = new Thread() {
                            @Override
                            public void run() {
                                try {
                                    for(int i=0;i<3;i++)
                                    {
                                        if(!smsStatus || !smsSendStatus){
                                            Log.e("@@","Inside Run..");
                                            int waited = 0;
                                            while( (waited < 120000)) {
                                                sleep(100);
                                                waited += 100;
                                            }
                                            Log.e("after","120sec count="+count);
                                            if(!smsStatus)
                                                handler.sendEmptyMessage(0);
                                        }
                                    }
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                finally
                                {
                                    Log.e("Logs", "Inside stop of thread");
                                    stop();
                                }
                            }
                        };
                        splashTread.start();
                    }   
                })
                .setNegativeButton(R.string.do_later,new DialogInterface.OnClickListener() {   
                    public void onClick(DialogInterface dialog, int whichButton) {   
                        finish();
                    }   
                })
                .setCancelable(false).show();

And the Handler Code is

 private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        count += 1;
        if(count<=3){
            try{
                Log.e("@@","Inside handler try count="+count);
                if(myProgressDialog.isShowing())
                    myProgressDialog.dismiss();
            }catch(Exception e){
                e.printStackTrace();
            }
            //new CheckLoginStatus().execute();
            sendSMS();
            if(!myProgressDialog.isShowing()){
                myProgressDialog=new ProgressDialog(MyClass.this);
                myProgressDialog.setTitle("My Message\nStep 2 of 4");
                myProgressDialog.setMessage("Me Message\n" +
                        "Please wait... attempt "+(count));
                myProgressDialog.setCancelable(false);
                myProgressDialog.show();
            }
        }else if(count == 4){
            try{
                Log.e("@@","Inside handler else "+count);
                if(myProgressDialog.isShowing())
                    myProgressDialog.dismiss();
            }catch(Exception e){
                e.printStackTrace();
            }
            count = 1;



            final TextView message = new TextView(getBaseContext()); 
            final SpannableString s = new SpannableString(getBaseContext().getText(R.string.SMS_NOT_RECEIVED));
            Linkify.addLinks(s, Linkify.WEB_URLS);
            message.setText(s);
            message.setPadding(10, 0, 4, 0);
            message.setGravity(Gravity.CLIP_HORIZONTAL);
            message.setMovementMethod(LinkMovementMethod.getInstance());  

            new AlertDialog.Builder(MyClass.this)   
            .setTitle("My Message\nError")   
            .setView(message)
            .setPositiveButton(R.string.OK_TEXT,new DialogInterface.OnClickListener() {   
                public void onClick(DialogInterface dialog, int whichButton) {   
                    finish();
                }   
            }).setCancelable(false).show();
        }
    }
};

I think my problem statement is this Linkify.addLinks(s, Linkify.WEB_URLS);

Earlier it used to work without this statement. Please help

1

There are 1 answers

0
codinguser On BEST ANSWER

If your handler is inside an Activity class, then use that class as the context instead of getBaseContext() when creating your TextView.

That will use the Activity context and make your problem disappear.