0

I have this interface:

public interface SomeInterface {

    void doSomething();
}

I am trying to initialize it in Main activity and I am trying to "send it" to second activity:

private SomeInterface someInterface;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    someInterface = (SomeInterface) this;
    someInterface.doSomething();
}

I implemented interface into second activity.

public class SecondActivity extends Activity implements SomeInterface  {

    @Override
    public void doSomething() {

    }
}

But this is not working, I am getting follwing error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{package.name/package.name.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to package.name.AppInterface

What I am doing wrong? Thank you in advance.

Michalsx
  • 3,446
  • 5
  • 33
  • 46

6 Answers6

4

You need to implement the SomeInterface in your Activity class

someInterface = (SomeInterface) this;

here this refer to your Activity not the Interface as you haven't implemented it. Hence ClassCastException

Kunu
  • 5,078
  • 6
  • 33
  • 61
  • I see, sorry for my bad explanation. I want to use it for comunication between activities. I am trying to call "doSomething" in second activity. How to do that? – Michalsx Apr 03 '17 at 12:05
  • 1
    Use a model class refer to this [http://stackoverflow.com/a/19027202/3022836](http://stackoverflow.com/a/19027202/3022836) – Kunu Apr 03 '17 at 12:08
0

Try This:::

SomeInterface myListener=null;

public MainActivity(SomeInterface ml) {
    myListener = ml;
}

and implement it in your MainActivity

public void SomeMethod(){
//Some Code here;
myListener.doSomething("passed");
}

and then call it in Second

@Override
public void doSomething(String str) {
//you will get your passed text here in str.
}
Mansi Bhatt
  • 214
  • 3
  • 12
0

I used custom model suggested by Kunu:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(this, "MainActivity", Toast.LENGTH_LONG).show();
        CustomModel.getInstance().changeState(true);
    }
}

.

public class SecondActivity extends Activity implements CustomModel.OnCustomStateListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this, "SecondActivity", Toast.LENGTH_LONG).show();
        CustomModel.getInstance().setListener(this);
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    public void stateChanged() {
        Toast.makeText(this, "stateChanged", Toast.LENGTH_LONG).show();
    }
}

First I launch second activity, after main, I kept this confusion order to keep it same as it was in question.

Community
  • 1
  • 1
Michalsx
  • 3,446
  • 5
  • 33
  • 46
0
public class YourActivity extends Activity implements SomeInterface {

  private SomeInterface someInterface;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // please add correct reference 
      someInterface = (SomeInterface) new SecondActivity();

      someInterface.doSomething();
  }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Farid Haq
  • 3,728
  • 1
  • 21
  • 15
-1

You need to implement the interface.. Try this

public class YourActivity extends Activity implements SomeInterface {

  private SomeInterface someInterface;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      someInterface = (SomeInterface) this;
      someInterface.doSomething();
  }
}
Prasanna Anbazhagan
  • 1,693
  • 1
  • 20
  • 37
  • I see, sorry for my bad explanation. I want to use it for comunication between activities. I am trying to call "doSomething" in second activity. How to do that? – Michalsx Apr 03 '17 at 12:05
-2

I will take what you wanna do is to once doSomething in first activity changed, you second activity would recognize it.

Then you could try this

public Interface SomeInterface
{
   void doSomething(); // here you may pass in some para maybe you want to use it to communicate with other activties
}

then in the first activity you get the instance of the interface

 public class FirstActivity extends AppCompatActivity
 {
    SomeInterface someInterface;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    someInterface = (SomeInterface) this;
    someInterface.doSomething();
}
}

In the second activity implement the interface

public void SecondActivity implements SomeInterface
{
   @Override
   public void doSomething()
   {
      // here it will run once doSomething is called in first activity,and if the interface in first activity has params, it would be passed here
   }
}
Mogician Ha
  • 85
  • 1
  • 2
  • 10