【问题标题】:How to go one Fragment to another when I will click on TextView Id?当我单击 TextView Id 时,如何将一个片段转到另一个片段?
【发布时间】:2014-11-20 06:52:39
【问题描述】:

当我点击TextView Id 时,如何将一个Fragment 转到另一个?

错误显示:

java.lang.ClassCastException: com.example.tripigator.Overview cannot be cast to android.app.Activity.

错误来自以下几行:

TextView overview = (TextView) findViewById(R.id.txt_overview);
            overview.setOnClickListener(new View.OnClickListener() {                
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(ProjectOverview.this, Overview.class);
                    startActivity(intent);              
                }
            });

所以请任何人帮助我如何通过单击文本项从一个 fragment 移动到另一个。

ProjectOverview 类:

public class ProjectOverview extends FragmentActivity {     
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.project_overview);
         initialingpaging();             
        TextView overview = (TextView) findViewById(R.id.txt_overview);
        overview.setOnClickListener(new View.OnClickListener() {                
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ProjectOverview.this, Overview.class);
                startActivity(intent);              
            }
        });
    }

     private void initialingpaging() {
            // TODO Auto-generated method stub
            List<Fragment> fragments = new Vector<Fragment>();
            fragments.add(Fragment.instantiate(this, Overview.class.getName()));
            fragments.add(Fragment.instantiate(this, Highlight.class.getName()));               
            mPageAdapter = new PageAdapter(this.getSupportFragmentManager(), fragments);            
            ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
            pager.setAdapter(mPageAdapter);     

        }
}

两个片段类:

public class Overview extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup container, Bundle savedInstanceState) 
    {    
        if(container == null)
        {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.overview,container,false);
    }
}


public class Highlight extends Fragment {       
    @Override
    public View onCreateView(LayoutInflater inflater,
             ViewGroup container, Bundle savedInstanceState) {              
        if(container == null)
        {
            return null;
        }
        return (LinearLayout) inflater.inflate(R.layout.highlight,container,false);
    }
}

布局界面:

<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" >
        <LinearLayout
            android:id="@+id/project_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginTop="65dp"          
            android:gravity="center" >

            <TextView
                android:id="@+id/txt_overview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_marginLeft="12dp"
                android:text="Overview" />

            <TextView
                android:id="@+id/txt_highlite"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_marginLeft="12dp"
                android:text="Highlight" />
        </LinearLayout>      
    </RelativeLayout>  
</RelativeLayout>

【问题讨论】:

标签: java android


【解决方案1】:

您无法使用 Intent 启动片段。试试这个它会有所帮助。

Fragment fragment = null;
FragmentManager frgManager;
FragmentTransaction ft;

fragment = new Overview();
ft = frgManager.beginTransaction();
ft.replace(R.id.frame_content, fragment);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();

如果你使用 View-Pager

your_ViewPager.setCurrentItem(position)

【讨论】:

    【解决方案2】:

    你不应该在片段中使用意图来移动另一个片段

    并检查整个示例 http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ 用这个。

    ((yourfragmentActivity) getActivity()).setCurrentItem(1,//desired activity 0=first 1= second and so on
                            true);
    

    插入

    Intent intent = new Intent(ProjectOverview.this, Overview.class);
                    startActivity(intent);
    

    【讨论】:

    • @MS Gadag 请发给我完整的代码,根据上面的代码将一个片段移动到另一个片段。请
    猜你喜欢
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多