Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选项卡碎片java onclick方法不工作,运行时没有错误_Java_Android - Fatal编程技术网

选项卡碎片java onclick方法不工作,运行时没有错误

选项卡碎片java onclick方法不工作,运行时没有错误,java,android,Java,Android,我有一个简单的主java“GameMode”,它将显示3个选项卡片段,其中一个是“HkmjSetting”(将在下面显示),现在当我运行应用程序时,它工作正常,但任何单击按钮都没有响应。在android studio中未发现红线 游戏模式java: public class GameMode extends AppCompatActivity { private ViewPager mViewPager; @Override protected void onCreate(Bundle sa

我有一个简单的主java“GameMode”,它将显示3个选项卡片段,其中一个是“HkmjSetting”(将在下面显示),现在当我运行应用程序时,它工作正常,但任何单击按钮都没有响应。在android studio中未发现红线

游戏模式java:

public class GameMode extends AppCompatActivity {

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_mode);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_game_mode, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


public static class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    public PlaceholderFragment() {
    }


    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       if (getArguments().getInt(ARG_SECTION_NUMBER)==1) {
           View rootView = inflater.inflate(R.layout.fragment_hkmj_setting, container, false);
           return rootView;
       } else if (getArguments().getInt(ARG_SECTION_NUMBER)==2) {
           View rootView = inflater.inflate(R.layout.fragment_quickmj_setting, container, false);
           return rootView;

       } else {
           View rootView = inflater.inflate(R.layout.fragment_hkmj_setting, container, false);
          // TextView textView = (TextView) rootView.findViewById(R.id.section_label);
           //textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
           return rootView;
       }
    }
}


public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

  }
}
香港中文大学

public class HkmjSetting extends Fragment {

Button btn_next;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_hkmj_setting, container, false);

    btn_next = (Button) view.findViewById(R.id.btn_next);
    btn_next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           /* Intent intent = new Intent(v.getContext(), MainActivity.class);
            startActivity(intent);*/
            Toast.makeText(getActivity(), "TESTING", Toast.LENGTH_SHORT).show();
        }
    });
    return view;
   }

}
运行结果没有错误,但单击“btn_next”时不会发生任何事情,这对你们中的许多人来说可能是一个愚蠢的问题,但请帮助…谢谢

2018-05-08更新Xml布局代码如评论所示,为了便于阅读,我简化了一点 fragment_hkmj_setting.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameSetting">

<!-- TODO: Update blank fragment layout -->
<android.support.constraint.ConstraintLayout 
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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HkmjSetting">

    <LinearLayout
        android:id="@+id/edtxList_6to10"
        android:layout_width="57dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.72"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.602">

        <TextView
            android:id="@+id/tv_10Fan"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginBottom="0dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/tv_10Fan"
            android:textColor="@color/ColorDropiii"
            android:textSize="18sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.024"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.982" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/ibtn_setting2"
        android:layout_width="58dp"
        android:layout_height="52dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="8dp"
        android:contentDescription="@string/tv_Setting"
        android:src="@drawable/hkmj"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tv_pageTitle"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    </LinearLayout>
    <Button
        android:id="@+id/btn_next"
        android:layout_width="139dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/ColorDropii"
        android:text="@string/btn_next"
        android:textColor="@color/ColorDropiv"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.876" />

</android.support.constraint.ConstraintLayout>
</FrameLayout>

activity\u game\u mode.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".GameMode">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_weight="1"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:title="@string/app_name">

</android.support.v7.widget.Toolbar>

<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.TabItem
        android:id="@+id/tabItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="香港麻雀" />

    <android.support.design.widget.TabItem
        android:id="@+id/tabItem2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跑馬仔" />

    <android.support.design.widget.TabItem
        android:id="@+id/tabItem3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="台灣麻雀" />

</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

将getItem()方法主体更改为if(position==2){return new HkmjSetting();}否则{return PlaceholderFragment.newInstance(position+1);}

正如@Mike M。
谢谢它起作用了

似乎是布局问题。你能发布
R.layout.fragment\u hkmj\u设置
R.layout.activity\u game\u模式
“3个选项卡片段,其中一个是“HkmjSetting””——这不是你的
部分SpagerAdapter所做的。返回的每个
片段
都是一个
占位符片段
,您没有在那里设置任何
OnClickListener
。谢谢@ADM,我已经更新了代码,请看一看,谢谢兄弟!很多@米肯。谢谢兄弟,我正在努力理解,当点击标签时效果很好,只是内容没有响应,我是在正确的轨道上还是出了什么问题?是的,标签本身会按预期工作,但页面都是
占位符片段
s,因为这就是您从
SectionsPagerAdapter
getItem()
方法返回的所有内容。从未使用过
HKMJ设置
片段
,因此您在那里进行的设置不会产生任何效果。听起来好像您希望
适配器中的三个
片段
中的一个成为
HKMJ设置
,因此,无论哪一个是,在
getItem()
中为给定的
位置
返回该片段的实例,而不是
占位符片段