【发布时间】: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