elixir bash
elixir bash

Reputation: 280

Call startActivity of a activity class from a different class(overlay)

i have Target.java(it is the main activity) and Layout.java. When user clicks a button a url opens. The button click is handled in Layout.java.

    deleteButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             DebugLog.LOGD("Within onClick event of 'GO' button ");
            Target.openURL();

        }
    });

Target.java has a static function openURL

    public static void openURL() {
     Uri uri = Uri.parse("http://www.thecinema.in");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);

     }

I'd like to know how i can make this code work, since the error im getting now is "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity" . I can understand startActivity will not work within a static function, but please suggest me how i could acheive the objective ..Im a newbie to android.FYI ..Thanks guys

-Updated code : Layout.java

    deleteButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
             DebugLog.LOGD("Within onClick event of 'GO' button ");
            Target iA = new Target();
            iA.openURL();


        }
    });

Target.java

     public void openURL() {    //removed static keyword
     DebugLog.LOGD("Within nDelete event of GO button ");

     Uri uri = Uri.parse("http://www.thecinema.in");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(intent);

 }

Exception thrown:

    03-02 10:10:28.351: D/QCAR(6820): Within onClick event of 'GO' button 
    03-02 10:10:28.351: W/dalvikvm(6820): threadid=1: thread exiting with uncaught exception (group=0x400207d8)
    03-02 10:10:28.361: E/AndroidRuntime(6820): FATAL EXCEPTION: main
    03-02 10:10:28.361: E/AndroidRuntime(6820): java.lang.NullPointerException
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.app.Activity.startActivityForResult(Activity.java:2817)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.app.Activity.startActivity(Activity.java:2923)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at com.ingage.pocs.phonegap.Layout$4.onClick(Layout.java:124)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.view.View.performClick(View.java:2408)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.view.View$PerformClick.run(View.java:8816)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.os.Handler.handleCallback(Handler.java:587)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at  android.os.Handler.dispatchMessage(Handler.java:92)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.os.Looper.loop(Looper.java:123)
    03-02 10:10:28.361: E/AndroidRuntime(6820):     at android.app.ActivityThread.main(ActivityThread.java:4633)

Guys what went wrong in here..

Upvotes: 2

Views: 7699

Answers (3)

elixir bash
elixir bash

Reputation: 280

i was able to come up with a solution for this question. Under Target.java i created openURL() as:

     public void openURL(Context context) 
    {
     Intent intent = new Intent("com.ingage.pocs.phonegap.URL");
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(intent);
     }

I created a new Activity OpenURL.java and associated the intent "com.ingage.pocs.phonegap.URL" with it:

    package com.ingage.pocs.phonegap;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;

    public class OpenURL extends Activity{

        @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    DebugLog.LOGD("Within BookTicketsActivity Activity GO button ");

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.thecinema.in"));
    startActivity(browserIntent);

}

}

Layout.java:

    private Context applicationContext;

    deleteButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
         DebugLog.LOGD("Within onClick event of 'GO' button ");
        ImageTargets ia =new ImageTargets();
             ia.openURL(applicationContext);
        iA.openURL();


    }
});

The best thing is, it worked :) If you guys come up with a better or more sensible solution , pls do post here.thank you

Upvotes: 6

Bartosz Przybylski
Bartosz Przybylski

Reputation: 1656

startActivity is not static and since you call it from Target not Layout it cannot resolve call context.

You'll need to call startActivity from Layout.java or pass reference or Target to Layout class

do it like this

Layout.java:

deleteButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
         DebugLog.LOGD("Within onClick event of 'GO' button ");
         Target.openURL(Layout.this);
    }
});

Target.java:

 public static void openURL(Context context) {
     Uri uri = Uri.parse("http://www.thecinema.in");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     context.startActivity(intent);
 }

Upvotes: 2

gobernador
gobernador

Reputation: 5739

You have

Targets iA = new Targets();

Did you, perhaps, mean to write

Target iA = new Target(); 

That would throw a NullPointerException because you're asking Java to find a class named Targets when none exists.

Upvotes: 1

Related Questions