使用actionBarSherlock通过按钮将活动链接到片段活动

时间:2013-06-04 03:04:49

标签: android eclipse actionbarsherlock adt android-fragmentactivity

我创建了一个项目,当您单击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);
        }
    }) ;

这是我的第3页代码段,其中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)

Here's the MainActivity

Here's the page3(including page3a to page3d fragments

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

2 个答案:

答案 0 :(得分: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 
    }

答案 1 :(得分:0)

有趣的(对你而言)部分是FATAL EXCEPTION

之后的内容

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

这与我提供的代码一样多。

在尝试使用标签和ViewPager的问题复杂化之前,我会尝试让简单的东西工作