0

我做了一个项目,其中当您单击 MainActivity 中的按钮时,它将导致片段活动。但我似乎无法让它工作。

这是我在 MainActivity 中的按钮的代码片段:

Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener () {

        public void onClick(View v) {

            Intent intent = new Intent (v.getContext(), page3.class);
            startActivityForResult(intent, 0);
        }
    }) ;

这是我的 page3 片段,其中 MainActivity 的按钮指向:

package com.example.pixelapp3;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class page3 extends SherlockFragmentActivity {

private ViewPager mViewPager;
private TabsAdapter mTabsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    setContentView(mViewPager);

    final ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment 1"), page3a.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment 2"), page3b.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment 3"), page3c.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment 4"), page3d.class, null);
}

}

这是 page3.xml 片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".mainActivity" >

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pager"/>

</RelativeLayout>

这是page3a.java:

package com.example.pixelapp3;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragmentActivity;


public class page3a extends SherlockFragmentActivity {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.page3a, null);
}

}

当我单击不起作用的按钮时,这是 logCat:

  06-04 10:46:14.871: I/ActionBarSherlock(907): Registering ActionBarSherlockCompat with qualifier @com.actionbarsherlock.ActionBarSherlock$Implementation(api=7, dpi=-1)
06-04 10:46:14.871: I/ActionBarSherlock(907): Registering ActionBarSherlockNative with qualifier @com.actionbarsherlock.ActionBarSherlock$Implementation(api=14, dpi=-1)
06-04 10:46:14.971: I/ActionBarSherlock(907): Using implementation: ActionBarSherlockNative
06-04 10:46:14.971: D/ActionBarSherlock(907): [<ctor>] activity: com.example.pixelapp3.page3@41219338, flags: 1
06-04 10:46:14.971: D/ActionBarSherlock(907): [setContentView] view: android.support.v4.view.ViewPager@4121c920
06-04 10:46:14.971: D/ActionBarSherlock(907): [setContentView] view: android.support.v4.view.ViewPager@4121c920, params: android.view.ViewGroup$LayoutParams@4122b4f0
06-04 10:46:15.212: D/ActionBarSherlock(907): [getActionBar]
06-04 10:46:15.222: D/ActionBarSherlock(907): [getActionBar]
06-04 10:46:15.361: V/21st Polling:(907): clicked
06-04 10:46:15.671: D/dalvikvm(907): GC_CONCURRENT freed 178K, 4% free 8234K/8519K, paused 74ms+88ms, total 260ms
06-04 10:46:15.671: D/AndroidRuntime(907): Shutting down VM
06-04 10:46:15.681: W/dalvikvm(907): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
06-04 10:46:15.761: E/AndroidRuntime(907): FATAL EXCEPTION: main
06-04 10:46:15.761: E/AndroidRuntime(907): java.lang.ClassCastException: com.example.pixelapp3.page3a cannot be cast to android.support.v4.app.Fragment
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
06-04 10:46:15.761: E/AndroidRuntime(907):  at com.example.pixelapp3.TabsAdapter.getItem(TabsAdapter.java:97)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:829)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.support.v4.view.ViewPager.populate(ViewPager.java:979)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.View.measure(View.java:15172)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.View.measure(View.java:15172)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.View.measure(View.java:15172)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 10:46:15.761: E/AndroidRuntime(907):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.View.measure(View.java:15172)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.Choreographer.doFrame(Choreographer.java:525)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.os.Handler.handleCallback(Handler.java:615)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:137)
06-04 10:46:15.761: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-04 10:46:15.761: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
06-04 10:46:15.761: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:511)
06-04 10:46:15.761: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-04 10:46:15.761: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-04 10:46:15.761: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)

这是 MainActivity

这是 page3(包括 page3a 到 page3d 片段

我已经被困在这里好几天了。任何类型的帮助表示赞赏。提前致谢!

4

2 回答 2

0

尝试使用此按钮实现:

 in your layout : 
       <Button
        android:id="@+id/btnLogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="yourmethod"
        android:text="button2" />

your code :

     public void yourmethod(View v){
   //do somthing on button press 
    }
于 2013-06-04T13:54:57.190 回答
0

(对您而言)有趣的部分是以下内容FATAL EXCEPTION

您似乎正在设置 ViewPager,并且在此过程中的某个时刻,您试图将类型的对象强制com.example.pixelapp3.page3a转换为 Fragment 类。由于对象不是片段,因此它会引发异常。

从您提供的代码中我可以看出这一点。

在尝试使用选项卡和 ViewPager 使问题复杂化之前,我会尝试让简单的东西正常工作

于 2013-06-04T14:05:58.760 回答