Android button 对象实例化失败,怎么解决啊

MainActivity.java的代码部分

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

static FragmentManager fragmentManager;
Button Sport;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragmentManager = getSupportFragmentManager();
    Toolbar toolbar = findViewById(R.id.toolbar);
    TabLayout tabLayout = findViewById(R.id.tablayout);
    toolbar.setTitle("首页");
    switchFragment(new MainFragment());
    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switch (tab.getPosition()) {
                case 0:
                    toolbar.setTitle(tab.getText());
                    switchFragment(new MainFragment());
                    break;
                case 1:
                    toolbar.setTitle(tab.getText());
                    switchFragment(new ShopFragment());
                    break;
                case 2:
                    toolbar.setTitle(tab.getText());
                    switchFragment(new MyFragment());
                    break;
            }
        }

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

        }

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

        }
    });
    Sport = findViewById(R.id.Sport);     //Sport报错
    Sport.setOnClickListener(this);

}

public static void switchFragment(Fragment fragment) {
    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}

@Override
public void onClick(View v) {
    
}

}

activity_main.xml的代码部分

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/danlan"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>
    </LinearLayout>

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tablayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_home_black_24dp"
            android:text="首页" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@drawable/ic_dashboard_black_24dp"
            android:text="商城" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:icon="@android:drawable/sym_def_app_icon"
            android:text="我的" />
    </com.google.android.material.tabs.TabLayout>

</LinearLayout>

fragment_main.xml部分

img

代码报错部分

img

  • 这篇博客: android studio创建android项目(1)——点击按钮跳转到新的Activity中的 1、修改布局文件activity_main.xml,添加Button 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.gui.helloworld.MainActivity">
    
        <Button
            android:id="@+id/btn_welcome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="欢迎功能"
            />
    </RelativeLayout>
    
    

button按钮你是放在了MainFragment中的啊,又不是放在装Fragment的Activity中,当然找不到了。你要去MainFragment中去做Sport按钮的相关操作