TestActivity にある myMethod を使用しようとしていますが、アプリケーションがクラッシュします。MainActivity 内に同じメソッドを配置してから使用する場合
((MainActivity) getActivity()).myMethod();
コードは機能しますが、TestActivity で見つかったメソッドを呼び出すことはできませんか? ありがとう
私のメイン アクティビティ パッケージ info.androidhive.tabsswipe;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
...
}
ゲームフラグメント
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();
}
丸太の猫
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)