我的浏览器对我正在处理的Android应用程序有问题。我有DrawerLayout调用Fragments。其中一个片段是HomeFragment,其中包含2个可单击的textview。我希望HomeFragment切换到另一个具有可滑动视图的Fragment(ParkingTutorialFragment)。我正在使用的代码切换到ParkingTutorialFragment但可刷卡的视图似乎没有工作,因为我刷卡时没有任何反应。下面是片段和适配器的代码:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class ParkingTutorialFragment extends Fragment {
private ViewPager viewPager;
private int page;
private String title;
public static ParkingTutorialFragment newInstance(int page, String title) {
ParkingTutorialFragment parkingFragment = new ParkingTutorialFragment();
Bundle args = new Bundle();
args.putInt("someInt", page);
args.putString("someTitle", title);
parkingFragment.setArguments(args);
return parkingFragment;
}
// Store instance variables based on arguments passed
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// page = getArguments().getInt("someInt", 0);
// title = getArguments().getString("someTitle");
}
// Inflate the view for the fragment based on layout XML
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.parking_tutorial_page, container, false);
return rootView;
}
}
请注意:
page = getArguments().getInt("someInt", 0);
上面的行返回null,我不知道为什么如果有人也可以告诉我为什么它无法检索值的关键字" someInt"如果有人也可以指出这一点我会非常感激
import java.util.LinkedHashMap;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
//Fragment for the home page
public class HomeFragment extends Fragment {
private TypedArray tutorial_icons;
private TextView tutorial_1;
private ViewPager viewpager;
FragmentPagerAdapter adapterViewPager;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
viewpager = (ViewPager) rootView.findViewById(R.id.tabs_pager);
adapterViewPager = new TabsPagerAdapter(getFragmentManager());
viewpager.setAdapter(adapterViewPager);
tutorial_1 = (TextView) rootView.findViewById(R.id.tutorial_item_1);
tutorial_1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
ParkingTutorialFragment parkingFrag = new ParkingTutorialFragment();
fragmentTransaction2.addToBackStack("Back");
fragmentTransaction2.hide(HomeFragment.this);
fragmentTransaction2.add(android.R.id.content, parkingFrag);
fragmentTransaction2.commit();
}
});
return rootView;
}
}
以下是适配器的代码:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 3;
public TabsPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return ParkingTutorialFragment.newInstance(0, "Page # 1");
case 1: // Fragment # 0 - This will show FirstFragment different title
return new OffersFragment();
default:
return ParkingTutorialFragment.newInstance(0, "Page # 1");
}
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
流程是HomeFragment - >按下按钮 - >转到ParkingTutorialFragment(应该包含一个swipeview) 如果任何人都可以帮助我实现所需的功能并澄清.getInt方法中的空指针,那将非常感激。谢谢。
答案 0 :(得分:0)
如果我理解正确的话,这将会解决问题。在按钮点击内更新你的代码。
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
fragmentTransaction2.addToBackStack("Back");
fragmentTransaction2.hide(HomeFragment.this);
fragmentTransaction2.add(android.R.id.content, ParkingTutorialFragment.newInstance(0, "Page # 1"));
fragmentTransaction2.commit();