添加另一个项目后,应用程序崩溃 - cwac-pager

时间:2018-02-04 15:53:21

标签: android commonsware-cwac android-pageradapter commonsware


我在向cwac-pager ArrayPagerAdapter v4 PagerAdapter添加其他页面时遇到问题。我不得不使用该库,因为我无法使用系统MainActivity.java动态添加新选项卡 public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener { private ViewPager viewPager; private ArrayPagerAdapter pagerAdapter; private TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setSupportActionBar((Toolbar) findViewById(R.id.app_toolbar)); tabLayout = findViewById(R.id.tab_layout); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); viewPager = findViewById(R.id.pager); pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), new ArrayList<PageDescriptor>()); viewPager.setAdapter(pagerAdapter); viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); tabLayout.addOnTabSelectedListener(this); } @Override public void onStart() { super.onStart(); pagerAdapter.add(new TestPageDescriptor()); tabLayout.addTab(tabLayout.newTab().setText("Hello")); // Uncomment the following lines to make the app crash pagerAdapter.add(new TestPageDescriptor()); // CRASH tabLayout.addTab(tabLayout.newTab().setText("Hello2")); } @Override public void onStop() { super.onStop(); // Remove all the tabs (required in my main application, not in this test) tabLayout.removeAllTabs(); for (int i = 0; i < pagerAdapter.getCount(); i++) { pagerAdapter.remove(i); } } @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } private class CustomPagerAdapter extends ArrayPagerAdapter<Fragment> { CustomPagerAdapter(FragmentManager fragmentManager, List<PageDescriptor> descriptors) { super(fragmentManager, descriptors); } @Override protected Fragment createFragment(PageDescriptor desc) { return new TestFragment(); } } private class TestPageDescriptor extends SimplePageDescriptor { TestPageDescriptor() { super("Test","TestHey"); } } }

TestFragment.java

public class TestFragment extends Fragment { @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.test_frag, container, false); } }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="me.test.myapplication.MainActivity" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/app_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:title="@string/app_name" /> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:elevation="4dp" android:minHeight="?attr/actionBarSize"/> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent" /> </LinearLayout>

test_frag.xml

LinearLayout:只有View MainActivity.java,无论是哪个。

onStart()中查看我在I/Process: Sending signal. PID: 3877 SIG: 9 Application terminated. 中添加的评论:如果您只使用一个标签运行应用程序(方法的最后两行已注释),则活动将正确启动。但是,如果通过取消注释行添加另一个选项卡,应用程序将立即崩溃。我做错了什么?
感谢

编辑:logcat

$(".custodianautocomplete").change(function() {
        var IDSelected = $(this).val();
        var recovered = "Rakesh";
        // $(this).parent("td").next().find("input[name=hall_location]").val("Rakesh") ;
        //window.alert($(this).parent("td").next().find("input[name=hall_location]").val());

        $.ajax({
             url: 'getEmpName.jsp',
             type : 'GET',
             data : {IDSelected: IDSelected},
             async: false,
             success : function(response) {
                recovered = response.trim();
                },
            fail:function (jqXHR, exception) {
                console.log(jqXHR);
                },
        });
        $(this).parent("td").next().find("input[name=hall_location]").val(recovered);
      });

没有例外,没有错误。

1 个答案:

答案 0 :(得分:1)

片段代码必须是唯一的,如the library documentation中所述。因此,正如Matt Clark指出的那样,您需要为不同的页面使用不同的标签。

请注意,您至少在问题的代码中不需要创建自己的SimplePageDescriptor子类。您可以直接使用SimplePageDescriptor