其實我正在用滑動選項卡製作應用程序。下載了滑動標籤的代碼。它有3個片段和MainActivity,它擴展了FragmentActivity,並使用viewpager。滑動標籤工作正常。現在我正在嘗試在其中一個片段中添加一個按鈕,並在點擊該按鈕時顯示敬酒。下面是我在XML文件中添加的片段代碼:按鈕單擊不起作用的片段
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fa6a6a" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Design Top Rated Screen"
android:textSize="20dp"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn_frgmnt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go to actvity"
android:onClick="navToActivity"
/>
</RelativeLayout>
而對於這個片段java文件裏面我做:
public class TopRatedFragment extends Fragment
{
Button btnNav;
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//btnNav = (Button)rootView.findViewById(R.id.btn_frgmnt);
context = getActivity().getApplicationContext();
}
public void navToActivity(View v)
{
Toast.makeText(getActivity().getApplicationContext(), "Clicked inside fragment!", 2000).show();
}
}
但其顯示的錯誤!我哪裏錯了。或者做點什麼來使按鈕點擊工作。它的這個片段類,我應該編寫按鈕點擊的代碼,對吧?
08-23 11:58:31.385: E/AndroidRuntime(3033): FATAL EXCEPTION: main
08-23 11:58:31.385: E/AndroidRuntime(3033): Process: info.androidhive.tabsswipe, PID: 3033
08-23 11:58:31.385: E/AndroidRuntime(3033): java.lang.IllegalStateException: Could not find a method navToActivity(View) in the activity class info.androidhive.tabsswipe.MainActivity for onClick handler on view class android.widget.Button with id 'btn_frgmnt'
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.view.View$1.onClick(View.java:3810)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.view.View.performClick(View.java:4438)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.view.View$PerformClick.run(View.java:18422)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.os.Handler.handleCallback(Handler.java:733)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.os.Handler.dispatchMessage(Handler.java:95)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.os.Looper.loop(Looper.java:136)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-23 11:58:31.385: E/AndroidRuntime(3033): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 11:58:31.385: E/AndroidRuntime(3033): at java.lang.reflect.Method.invoke(Method.java:515)
08-23 11:58:31.385: E/AndroidRuntime(3033): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-23 11:58:31.385: E/AndroidRuntime(3033): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-23 11:58:31.385: E/AndroidRuntime(3033): at dalvik.system.NativeStart.main(Native Method)
08-23 11:58:31.385: E/AndroidRuntime(3033): Caused by: java.lang.NoSuchMethodException: navToActivity [class android.view.View]
08-23 11:58:31.385: E/AndroidRuntime(3033): at java.lang.Class.getConstructorOrMethod(Class.java:472)
08-23 11:58:31.385: E/AndroidRuntime(3033): at java.lang.Class.getMethod(Class.java:857)
08-23 11:58:31.385: E/AndroidRuntime(3033): at android.view.View$1.onClick(View.java:3803)
08-23 11:58:31.385: E/AndroidRuntime(3033): ... 11 more
[onClick on fragment on called on Activity]的可能重複(http://stackoverflow.com/questions/7570575/onclick-inside-fragment-called-on-activity) – Simas