Linking from an activity to a fragment activity wi

2019-09-15 06:10发布

I made a project wherein when you click a button from the MainActivity, it will lead to a fragment activity. But I can't seem to make it work.

Here's a snippet of my code for the button in 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);
        }
    }) ;

Here's my snippet for page3 where the button from MainActivity leads to:

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);
}

}

Here's the page3.xml snippet:

<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>

Here's 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);
}

}

Here's the logCat when I click the button that doesn't work:

  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

I've been stuck here for days. Any type of help is appreciated. Thanks in advance!

2条回答
劫难
2楼-- · 2019-09-15 06:15

Try using this button implementation instead :

 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 
    }
查看更多
我命由我不由天
3楼-- · 2019-09-15 06:28

The interesting (to you) part is what follows FATAL EXCEPTION

You appear to be setting up a ViewPager and at some point in this process you are trying to cast an object of type com.example.pixelapp3.page3a to a Fragment class. As the object is not a fragment, it is throwing an exception.

That is as much as I can tell from the code you provided.

I would try to get the simple stuff working before attempting to complicate matters with tabs and ViewPager's

查看更多
登录 后发表回答