Not able to use button as it is on another layout

こ雲淡風輕ζ 提交于 2020-07-10 10:27:00

问题


My Main Activity. This is where all my coding is done.

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) {

        }
    });
}

}

My Main Activity 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>

My Third Fragment XML. This is my third fragent and where my button is located.

<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>

I am trying to use a button which is on my third layout to show a add and when I try to use my button in my MainActivity it gives me a null object error.

My Third Fragment Activity

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);
    }

} 

I need a method that searches other layouts for Id's as currently, my code is only searching the MainActitivity for Id's and hence is unable to find my button. This is a very small thing that is causing very large issues for me and is ruining my life slowly.


回答1:


try to pass that class object to the class where u are trying to access because no instance is available in memory and you are trying to access container without initialising so its giving u null issue.




回答2:


You are trying to access the button, which is defined in Third Fragment XML, from Main Activity which has activity_main.xml as its resource file. Android will try to search bt_show in activity_main.xml. Since it's not defined there, you are getting an NPE.

You should use findViewById method in the onCreateView or onViewCreated method of the Third Fragment.

Hope this helps.



来源:https://stackoverflow.com/questions/62662917/not-able-to-use-button-as-it-is-on-another-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!