gpt4 book ai didi

java - 使用 Intent 从另一个 fragment 调用一个 fragment

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:03 25 4
gpt4 key购买 nike

我正在使用 viewpager 控件托管三个 fragment 。我在每个 fragment 中放置了一个按钮以转到下一个 fragment 。所以我想知道如何使用 Intent 从一个 fragment 转到另一个 fragment ?

主要目的是创建一个注册表单,分为三个 fragment ,代码如下:

主要 Activity :

package pl.looksok.viewpagerdemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class MainActivity extends FragmentActivity {
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyPagerAdapter pageAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager = (ViewPager)findViewById(R.id.myViewPager);
pager.setAdapter(pageAdapter);

}

}

第一个 fragment :

package pl.looksok.viewpagerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentBlue extends Fragment {
Button btnnext1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_blue, container, false);
btnnext1=(Button) view.findViewById(R.id.btnnext1);

btnnext1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), FragmentGreen.class);
view.getContext().startActivity(intent);
getActivity().finish();

}
});

return view;
}
}

第二个 fragment :

package pl.looksok.viewpagerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentGreen extends Fragment {
Button btnnext2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_green, container, false);
btnnext2=(Button) view.findViewById(R.id.btnnext2);
btnnext2.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), FragmentPink.class);
view.getContext().startActivity(intent);
getActivity().finish();

}
});
return view;
}
}

第三个 fragment :

package pl.looksok.viewpagerdemo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.support.v4.app.Fragment;

public class FragmentPink extends Fragment {
Button btnnext3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_pink, container, false);
btnnext3=(Button) view.findViewById(R.id.btnnext3);
btnnext3.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {


}
});
return view;
}
}

viewpager 的适配器:

package pl.looksok.viewpagerdemo;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyPagerAdapter extends FragmentPagerAdapter {

private List<Fragment> fragments;

public MyPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
fragments.add(new FragmentBlue());
fragments.add(new FragmentGreen());
fragments.add(new FragmentPink());
}

@Override
public Fragment getItem(int position) {
return fragments.get(position);
}

@Override
public int getCount() {
return fragments.size();
}
}

viewpager 的自定义类:

package pl.looksok.viewpagerdemo;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {

private boolean isPagingEnabled = false;

public CustomViewPager(Context context) {
super(context);
}

public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onTouchEvent(event);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return this.isPagingEnabled && super.onInterceptTouchEvent(event);
}

public void setPagingEnabled(boolean b) {
this.isPagingEnabled = b;
}
}

我现在使用的代码在单击第一个按钮时显示以下错误:

09-20 10:54:18.632: E/AndroidRuntime(9401): FATAL EXCEPTION: main
09-20 10:54:18.632: E/AndroidRuntime(9401): android.content.ActivityNotFoundException: Unable to find explicit activity class {pl.looksok.viewpagerdemo/pl.looksok.viewpagerdemo.FragmentGreen}; have you declared this activity in your AndroidManifest.xml?
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Activity.startActivityForResult(Activity.java:2827)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:824)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.Activity.startActivity(Activity.java:2933)
09-20 10:54:18.632: E/AndroidRuntime(9401): at pl.looksok.viewpagerdemo.FragmentBlue$1.onClick(FragmentBlue.java:25)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.view.View.performClick(View.java:2538)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.view.View$PerformClick.run(View.java:9152)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.os.Handler.handleCallback(Handler.java:587)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.os.Handler.dispatchMessage(Handler.java:92)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.os.Looper.loop(Looper.java:130)
09-20 10:54:18.632: E/AndroidRuntime(9401): at android.app.ActivityThread.main(ActivityThread.java:3689)
09-20 10:54:18.632: E/AndroidRuntime(9401): at java.lang.reflect.Method.invokeNative(Native Method)
09-20 10:54:18.632: E/AndroidRuntime(9401): at java.lang.reflect.Method.invoke(Method.java:507)
09-20 10:54:18.632: E/AndroidRuntime(9401): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-20 10:54:18.632: E/AndroidRuntime(9401): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-20 10:54:18.632: E/AndroidRuntime(9401): at dalvik.system.NativeStart.main(Native Method)

所以问题是如何在 fragment 之间使用 Intent 。有没有更好的方法将注册表单分成三部分而不使用viewpager?

最佳答案

你不能这样调用你的 fragment ,

  Intent intent = new Intent(view.getContext(), FragmentGreen.class);
view.getContext().startActivity(intent);
getActivity().finish();

你需要这样调用你的 fragment

    FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentGreen llf = new FragmentGreen();
ft.replace(R.id.listFragment, llf);
ft.commit();

Intent 基本上用于从另一个 Activity 调用一个 Activity 。要添加新的 Fragment,您不能使用 Intent。为此,您必须使用 FragmentManager 并打开 fragment FragmentTransaction

有关更多详细信息,请转到 Here

关于java - 使用 Intent 从另一个 fragment 调用一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25945613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com