【问题标题】:how to implement Buttons clickhandler on Fragments如何在片段上实现按钮点击处理程序
【发布时间】:2014-05-27 21:14:38
【问题描述】:

我仍然对如何在 Fragments 上实现按钮处理程序感到困惑。

这是我的片段:

package com.example.myshops_diary;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.Fragment;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater; 
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Item1_fragment extends Fragment implements OnClickListener { 

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.item1_fragment, container, false);

    Button nextpageButton = (Button) view.findViewById(R.id.nextpageButton);
nextpageButton.setOnClickListener(this);
return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.nextpageButton:
    Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();


        break;
    }

}

private void setContentView(int item2_Fragment) {
    // TODO Auto-generated method stub



    //---Inflate the layout for this fragment---
//return inflater.inflate( R.layout.item1_fragment, container, false);



}





}

这些代码仍然无法正常工作,当我单击“下一步”按钮时,应用程序停止运行。

** 这些是我的 LogCat:

04-14 13:00:15.899: D/dalvikvm(537): Not late-enabling CheckJNI (already on)
04-14 13:00:17.359: D/gralloc_goldfish(537): Emulator without GPU emulation detected.
04-14 13:00:28.289: D/AndroidRuntime(537): Shutting down VM
04-14 13:00:28.299: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-14 13:00:28.309: E/AndroidRuntime(537): FATAL EXCEPTION: main
04-14 13:00:28.309: E/AndroidRuntime(537): java.lang.IllegalStateException: Could not find a method nextClick(View) in the activity class com.example.myshops.MainActivity for onClick handler on view class android.widget.Button with id 'nextpageButton'
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View$1.onClick(View.java:3026)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View.performClick(View.java:3480)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View$PerformClick.run(View.java:13983)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.os.Handler.handleCallback(Handler.java:605)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:137)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:4340)
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)
04-14 13:00:28.309: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-14 13:00:28.309: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-14 13:00:28.309: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
04-14 13:00:28.309: E/AndroidRuntime(537): Caused by: java.lang.NoSuchMethodException: nextClick [class android.view.View]
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
04-14 13:00:28.309: E/AndroidRuntime(537):  at java.lang.Class.getMethod(Class.java:915)
04-14 13:00:28.309: E/AndroidRuntime(537):  at android.view.View$1.onClick(View.java:3019)
04-14 13:00:28.309: E/AndroidRuntime(537):  ... 11 more

【问题讨论】:

  • 你想做什么:在你的布局中添加另一个片段?
  • 不,只是为了让按钮工作......片段内的按钮很少
  • 好的,发布 logcat。我会尽力帮助你的。
  • ok,logcat 已经发布了,请帮帮我@Fllo

标签: android android-fragments onclicklistener android-button


【解决方案1】:

试试这个。

public static class PlaceholderFragment extends Fragment implements
        OnClickListener {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_job_seeker_home,
                container, false);
        Button bu = (Button) rootView.findViewById(R.id.next);
        bu.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.next:
        Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
            break;
        }
    }
}

【讨论】:

  • 你能分享你的xml布局吗?
【解决方案2】:

您的onClick 方法看起来不错。我不知道您尝试做什么,但是您不能设置内容视图两次。您已经在 onCreateView 方法中膨胀了一个视图:

inflater.inflate(R.layout.item1_fragment, container, false);

那么你不能在下面调用这个代码来添加布局。这是您崩溃的原因:

setContentView(R.layout.item2_fragment);  

还要确保在布局中单击的按钮具有此 ID:

<Button
    android:id="@+id/nextpageButton"
    ... />  

由于android:onClick="nextClick" 属性,您有此错误:NoSuchMethodException: nextClick。这意味着系统找不到名为 nextClick 的方法。您尝试调用按钮提供的onClickListener 方法 onClick 方法。

然后,删除布局中的属性并将这部分保留在 Activity 中:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        // This will display a Toast (msg) when the button is clicked
        case R.id.nextpageButton:
            Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
        break;
    }
}  

See the documentation 查看两种不同的方法。

更新

你设置点击监听如下:

// Here the inflated view (the layout) and the button are the same variable..
view = (Button) view.findViewById(R.id.nextpageButton);
view.setOnClickListener(this);    

但是你应该对你的按钮使用不同的变量,如下所示:

// Here we give another name variable as "button_name" and also we set the widget "Button"
Button button_name = (Button) view.findViewById(R.id.nextpageButton);
button_name.setOnClickListener(getActivity());  

解决方案

这里还有一个方法,直接调用onClickListener,不用实现。删除这个:

implements OnClickListener // on your Fragment Class declaration

然后:

nextpageButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
    }
});

解决方案2

移除你Activity中的所有onClick方法等。在布局中添加以下属性:

android:onClick="nextpageButton" // on your button  

然后在你的活动中

// call the "nextpageButton" inside your activity as
public void nextpageButton(View view) {
    Toast.makeText(getActivity().getApplicationContext(), "Button clicked!", Toast.LENGTH_LONG).show();
}

【讨论】:

  • 好的,现在试试...实际上我很关心按钮功能本身,它假设将页面定向到下一页,但从您更新的代码看来,它没有显示任何功能它完全移动到(导航)下一页@Fllo
  • 它不起作用..我认为它应该更改为将当前页面定向到下一页(其他片段)..@Fllo
  • @user3514625 解决方案2 在您的活动中,请参阅this answer,这肯定会起作用!
  • ...嗯...我现在不知道。我尝试了一切来理解。我只能说最后一件事:对不起,祝你好运;)我真的希望你能找到正确的方法。
【解决方案3】:

在您的 MainActivity 中,首先为片段充气,

MainFragment framgmentInstance=new MainFragment();
this.getSupportFragmentManager().beginTransaction().replace(R.id.content_main,framgmentInstance,"tag").commit();

片段布局中的按钮应该是这样的,

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="click"
    android:id="@+id/btnOne"
/>

据我了解,您想使用按钮单击事件打开另一个活动。如果我错了纠正我。但是,如果您想使用片段中的按钮打开新活动,请尝试以下代码。

Button btnOne=(Button)view.findViewById(R.id.btnOne);
    btnOne.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(getActivity(),secondActivity.class);
            startActivity(i);
        }
    });

'secondActivity' 是您要打开的活动的名称。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    相关资源
    最近更新 更多