Android 无法使用按钮,因为它位于其他布局上

Android 无法使用按钮,因为它位于其他布局上,android,android-studio,button,nullpointerexception,null,Android,Android Studio,Button,Nullpointerexception,Null,我的主要活动。这就是我所有编码的地方 FrameLayout simpleFrameLayout; TabLayout tabLayout; VideoView video; Button btn_show; InterstitialAd interstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

我的主要活动。这就是我所有编码的地方

FrameLayout simpleFrameLayout;
TabLayout tabLayout;
VideoView video;
Button btn_show;
InterstitialAd interstitialAd;




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

    btn_show = (Button) findViewById(R.id.bt_show);

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("ca-app-pub-8708219564656739/2401085524");
    interstitialAd.loadAd(new AdRequest.Builder().build());

    btn_show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            interstitialAd.show();
        }
    });

    // get the reference of FrameLayout and TabLayout
    simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
    tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
    // Create a new Tab named "First"
    TabLayout.Tab firstTab = tabLayout.newTab();
    firstTab.setText("Apps"); // set the Text for the first Tab
    firstTab.setIcon(R.drawable.app); // set an icon for the
    // first tab
    tabLayout.addTab(firstTab); // add  the tab at in the TabLayout
    // Create a new Tab named "Second"
    TabLayout.Tab secondTab = tabLayout.newTab();
    secondTab.setText("Products"); // set the Text for the second Tab
    secondTab.setIcon(R.drawable.company); // set an icon for the second tab
    tabLayout.addTab(secondTab); // add  the tab  in the TabLayout
    // Create a new Tab named "Third"
    TabLayout.Tab thirdTab = tabLayout.newTab();
    thirdTab.setText("Donate"); // set the Text for the first Tab
    thirdTab.setIcon(R.drawable.donation); // set an icon for the first tab
    tabLayout.addTab(thirdTab); // add  the tab at in the TabLayout


    // perform setOnTabSelectedListener event on TabLayout
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            // get the current selected tab's position and replace the fragment accordingly
            Fragment fragment = null;
            switch (tab.getPosition()) {
                case 0:
                    fragment = new FirstFragment();
                    break;
                case 1:
                    fragment = new SecondFragment();
                    break;
                case 2:
                    fragment = new ThirdFragment();
                    break;
            }
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.simpleFrameLayout, fragment);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
        }



        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}
}

我的主要活动是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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:clickable="true"
    android:focusable="true"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.TabLayout
        android:id="@+id/simpleTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@android:color/darker_gray"
        app:tabIndicatorColor="#f00"
        app:tabSelectedTextColor="#f00"
        app:tabTextColor="#000" />

    <FrameLayout
        android:id="@+id/simpleFrameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#9C27B0">

    </FrameLayout>

</LinearLayout>
我需要一个方法来搜索Id的其他布局,因为目前,我的代码只搜索Id的MainActivity,因此无法找到我的按钮。
这是一件非常小的事情,它给我带来了非常大的问题,并慢慢地毁掉了我的生活。

尝试将该类对象传递给您尝试访问的类,因为内存中没有可用的实例,您尝试访问容器而不进行初始化,因此它会给您零问题。

您尝试访问该按钮,它是在第三个XML片段中定义的,来自主活动,其资源文件为Activity_Main.XML。Android将尝试在activity_main.xml中搜索bt_show。因为这里没有定义,所以你得到了一个NPE

您应该在第三个片段的onCreateView或onViewCreated方法中使用findViewById方法


希望这有帮助。

确保您设置侦听器的按钮已初始化。另外,请将实际问题和问题描述放在顶部,我不知道您的按钮或它们在哪里或其他任何地方。
btn\u show
为空。检查您的
activity\u main.xml
以确保它包含ID为
bt\u show
的视图。我已经更新了问题,您可以再次检查吗?请详细说明!我试过了,但在第三个片段的方法中,findViewById似乎不是一个有效的操作。您必须在onviewcreated方法中使用view.findViewById
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rellay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00BCD4"
    tools:context="info.androidhive.materialtabs.fragments.OneFragment">

    <Button
        android:id="@+id/bt_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Ad"
        android:textAllCaps="false" />
    <com.google.android.gms.ads.AdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ad_view"
        android:layout_centerHorizontal="true"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-8708219564656739/7492209225"/>


    <com.google.android.gms.ads.AdView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ad_view2"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-8708219564656739/3552964218"/>


</RelativeLayout>
package com.abhiandroid.tablayoutexample;

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

public class ThirdFragment extends Fragment {


    public ThirdFragment() {
        // Required empty public constructor
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_third, container, false);
    }

}