我仍然对如何在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
答案 0 :(得分:0)
您的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();
}
答案 1 :(得分:0)
试试这个。
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;
}
}
}
答案 2 :(得分:0)
在MainActivity中,首先给片段充气,
<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);
}
});
据我所知,您想使用按钮点击事件打开另一个活动。如果我错了,请纠正我。但是,如果要使用片段中的按钮打开新活动,请尝试以下代码。
awk
&#39; secondActivity&#39;是您要打开的活动的名称。