我正在尝试使用TestActivity中的myMethod,但应用程序崩溃了。 如果我在MainActivity中放入相同的方法然后使用
((MainActivity) getActivity()).myMethod();
代码有效,但它不会让我调用TestActivity中找到的方法吗? 感谢
我的主要活动 包info.androidhive.tabsswipe;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
...
}
GamesFragment
public class GamesFragment extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_games, container, false);
Button showButton = (Button) rootView.findViewById(R.id.Btn_Show);
showButton.setOnClickListener (this);
return rootView;
}
public interface YourInterface {
public void yourMethod();
}
private YourInterface yourInterface ;
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try {
yourInterface = (TestActivity) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement YourInterface");
}
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.Btn_Show:
yourInterface .yourMethod();
break;
}
}
}
测试活动
public class TestActivity extends FragmentActivity implements OnClickListener, YourInterface {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lyt_test);
}
public void yourMethod(){
Toast.makeText(getApplicationContext(),"Text!",Toast.LENGTH_SHORT).show();
}
Log Cat
11-02 19:33:15.468: E/Trace(1952): error opening trace file: No such file or directory (2)
11-02 19:33:15.560: D/AndroidRuntime(1952): Shutting down VM
11-02 19:33:15.560: W/dalvikvm(1952): threadid=1: thread exiting with uncaught exception (group=0xa62fa288)
11-02 19:33:15.560: E/AndroidRuntime(1952): FATAL EXCEPTION: main
11-02 19:33:15.560: E/AndroidRuntime(1952): java.lang.ClassCastException: info.androidhive.tabsswipe.MainActivity@a6d853c0 must implement YourInterface
11-02 19:33:15.560: E/AndroidRuntime(1952): at info.androidhive.tabsswipe.GamesFragment.onAttach(GamesFragment.java:40)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.View.measure(View.java:15172)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.View.measure(View.java:15172)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.View.measure(View.java:15172)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-02 19:33:15.560: E/AndroidRuntime(1952): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.View.measure(View.java:15172)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.Choreographer.doFrame(Choreographer.java:525)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.os.Handler.handleCallback(Handler.java:615)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.os.Looper.loop(Looper.java:137)
11-02 19:33:15.560: E/AndroidRuntime(1952): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-02 19:33:15.560: E/AndroidRuntime(1952): at java.lang.reflect.Method.invokeNative(Native Method)
11-02 19:33:15.560: E/AndroidRuntime(1952): at java.lang.reflect.Method.invoke(Method.java:511)
11-02 19:33:15.560: E/AndroidRuntime(1952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-02 19:33:15.560: E/AndroidRuntime(1952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-02 19:33:15.560: E/AndroidRuntime(1952): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
11-02 19:33:15.560: E/AndroidRuntime(1952): java.lang.ClassCastException: info.androidhive.tabsswipe.MainActivity@a6d853c0 must implement YourInterface
更改
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
...
}
到
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener , YourInterface {
...
}
答案 1 :(得分:0)
你应该使用界面: 活性:
public class TestActivity extends FragmentActivity implements OnClickListener, YourInterface {
...
@Override
public void yourMethod(){
//smth to do
}
}
片段:
...
public interface YourInterface {
public void yourMethod();
}
private YourInterface yourInterface ;
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
try {
yourInterface = (YourInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement YourInterface");
}
}
...
public void onClick(View v) {
switch (v.getId()) {
case R.id.Btn_TUD_Show:
yourInterface .yourMethod();
break;
}
}
答案 2 :(得分:0)
以下是两项活动的答案。
测试活动:
...
private TestActivity instance;
public static TestActivity getTestActivity (){
if (instance == null) instance = new TextActivity ();
return instance;
}
public void onCreate(Bundle b){
...
instance = this;
...}
...
和片段:
TestActivity .getTestActivity ().yourMethod();