【发布时间】:2014-01-25 22:36:59
【问题描述】:
我的测试项目有一个主要活动如下:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
public void sendMessage(View view){
FragmentManager frManager = getFragmentManager();
FragmentTransaction transaction = frManager.beginTransaction();
Frag fr= new Frag();
transaction.add(R.id.stuuf, fr);
transaction.addToBackStack(null);
transaction.commit();
}
}
以及以下主要布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/stuuf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="?android:attr/actionBarSize" >
<Button android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>
</TableLayout>
当用户单击按钮时,会显示以下片段,其中包含第二个按钮:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Frag" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="180dp"
android:onClick="buttonClicked"
android:text="Button" />
</RelativeLayout>
以及应该控制它的类:
public class Frag extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag_test, null);
}
public void buttonClicked(){
System.out.println("Hello!");
}
}
现在,当我单击第二个按钮时,系统会在 MainActivity 中而不是在 Frag 中查找 buttonClicked() 方法,从而给我以下错误:
java.lang.IllegalStateExcetion: Could not find method buttonClicked(View) in the activity class com.spacitron.mytestproject.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
显然我应该能够从片段类本身处理片段中发生的事情,所以我一定是在做完全错误的事情,我只是无法确定是什么。
【问题讨论】:
标签: java android android-fragments