如何在Activity中正确初始化界面?

时间:2017-04-03 11:49:44

标签: android interface initialization

我有这个界面:

public interface SomeInterface {

    void doSomething();
}

我正在尝试在Main活动中初始化它,我正在尝试将其“发送”到第二个活动:

private SomeInterface someInterface;

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

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

我将接口实现为第二个活动。

public class SecondActivity extends Activity implements SomeInterface  {

    @Override
    public void doSomething() {

    }
}

但这不起作用,我得到了以下错误:

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

我做错了什么?提前谢谢。

6 个答案:

答案 0 :(得分:3)

您需要在Activity类

中实现SomeInterface

someInterface = (SomeInterface) this;

此处指的是您的活动而不是接口,因为您尚未实施它。 因此ClassCastException

答案 1 :(得分:0)

试试这个:::

SomeInterface myListener=null;

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

并在MainActivity中实现它

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

然后在第二个

中调用它
@Override
public void doSomething(String str) {
//you will get your passed text here in str.
}

答案 2 :(得分: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();
    }
}

首先,我发布了第二个活动,在主要之后,我保持这个混乱顺序,以保持它与有问题的相同。

答案 3 :(得分: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();
  }
}

答案 4 :(得分:-1)

您需要实现界面..试试这个

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

答案 5 :(得分:-1)

我会采取你想做的事情,一旦做了第一次活动的某些事情发生变化,你的第二个活动就会认出来。

然后你可以试试这个

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

然后在第一个活动中获得接口的实例

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

在第二个活动中实现界面

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