【发布时间】:2015-04-20 18:44:46
【问题描述】:
我正在尝试调用片段中定义的方法。例如我在片段中有以下方法
public void testMethod(){
Toast.makeText(getActivity(),"Hi",Toast.LENGTH_SHORT).show();
}
我想从 MainActivity 调用这个方法。我定义如下。
private void callMethodFromFragment() {
TestFragment testFragment = new TestFragment();
testFragment.testMethod();
}
我收到以下错误
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4000)
at android.view.View.performClick(View.java:4754)
这是我的 activity_main.xml 布局
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/>
</android.support.v4.widget.DrawerLayout>
【问题讨论】:
-
This 可能会有所帮助
-
我看到你可以从 id 或 tag 访问它。但我没有在 layout 中定义片段。我只有 FrameLayout 和 ListView
-
能否请您显示更多代码以及您拥有哪些布局文件?
标签: android android-intent android-fragments android-activity