Java Android:在片段中处理用户输入

Java Android:在片段中处理用户输入,java,android,android-fragments,Java,Android,Android Fragments,我的测试项目的主要活动如下: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override p

我的测试项目的主要活动如下:

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>
现在,有一个问题是,当我单击第二个按钮时,系统会在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'

显然,我应该能够处理来自fragment类本身的片段中发生的事情,所以我一定是做了一些完全错误的事情,我只是不能确定是什么。

看起来您缺少了一些步骤,请将其放在第二个片段中(并从xml中删除onClick)


看起来您缺少了一些步骤,请将其放在第二个片段中(并从xml中删除onClick)


问题是您在片段布局中使用了
onClick
方法,而Android希望在活动中找到这个方法。有两种方法可以解决此问题:

  • 最健康的方法是:从XML中删除
    onClick
    属性,并依赖
    OnClickListener
    实现
  • 将调用从活动委托给片段-在活动中声明方法并将其委托给片段。如下所示:
  • 在活动类中,请执行以下操作:

    public void buttonClicked(View view){
        Fragment current = getFragmentManager().findFragmentById(R.id.stuuf);
        if(current != null) {
            ((Frag)current).buttonClicked();
        }
    }
    

    从长远来看,我强烈建议不要使用
    onClick
    XML属性。原因已在中解释。

    问题在于您在片段布局中使用了
    onClick
    方法,而Android希望在活动中找到这一点。有两种方法可以解决此问题:

  • 最健康的方法是:从XML中删除
    onClick
    属性,并依赖
    OnClickListener
    实现
  • 将调用从活动委托给片段-在活动中声明方法并将其委托给片段。如下所示:
  • 在活动类中,请执行以下操作:

    public void buttonClicked(View view){
        Fragment current = getFragmentManager().findFragmentById(R.id.stuuf);
        if(current != null) {
            ((Frag)current).buttonClicked();
        }
    }
    

    从长远来看,我强烈建议不要使用
    onClick
    XML属性。原因在中解释。

    片段构建在活动之上,onClick属性与活动连接

    我建议您对这两个按钮使用ClickListeners:-

    @Override
    public void onViewCreated((View view, Bundle savedInstanceState) {
    Button fragmentButton = (Button)view.findViewById(R.id.button1);
    fragmentButton.setOnClickListener(new View.OnClickLisener() {
    
        public void onClick(View view) {
            //Do Something here
        }
    
    });
    
    }
    

    片段构建在活动之上,onClick属性与活动连接

    我建议您对这两个按钮使用ClickListeners:-

    @Override
    public void onViewCreated((View view, Bundle savedInstanceState) {
    Button fragmentButton = (Button)view.findViewById(R.id.button1);
    fragmentButton.setOnClickListener(new View.OnClickLisener() {
    
        public void onClick(View view) {
            //Do Something here
        }
    
    });
    
    }
    
    @Override
    public void onViewCreated((View view, Bundle savedInstanceState) {
    Button fragmentButton = (Button)view.findViewById(R.id.button1);
    fragmentButton.setOnClickListener(new View.OnClickLisener() {
    
        public void onClick(View view) {
            //Do Something here
        }
    
    });
    
    }