-10
我的接口初始化接口
public interface Baseface {
public void updatedName(String newName);
}
MainActivity
public class MainActivity extends AppCompatActivity {
private Baseface baseface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
baseface = (Baseface) this;
baseface.updatedName("hello");
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity
public class SecondActivity extends AppCompatActivity implements Baseface {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public void updatedName(String newName) {
Log.e("my new name is:",newName);
}
}
我有錯誤
Caused by: java.lang.ClassCastException. MainActivity cannot be cast to Baseface
研究以及有關的接口。 –
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – larsgrefer
使用接口進行活動間通信是一種錯誤的方法。 – Selvin