我想用switch语句跳转到其它activity,从而实现底部导航栏的效果,但当我在发布运行的时候编译器一直提示
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
我看了其他帖子,有的说是我没有初始化。但我却无法在代码中按正常初始化
这是我的MainActivity代码
package com.example.prolife;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.home).setOnClickListener(this);
findViewById(R.id.shop).setOnClickListener(this);
findViewById(R.id.user).setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.home:
Intent intent=new Intent();
intent.setClass(this,MainActivity.class);
startActivity(intent);
break;
case R.id.shop:
Intent intent1=new Intent();
intent1.setClass(this,shopActivity2.class);
startActivity(intent1);
break;
case R.id.user:
Intent intent2=new Intent();
intent2.setClass(this,userActivity2.class);
startActivity(intent2);
break;
}
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:text="@string/home_page"
android:textSize="30sp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/ic_line" />
LinearLayout>
<RelativeLayout
android:id="@+id/ll_function_btn"
android:layout_width="match_parent"
android:layout_height="414dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_take"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_take"
android:textSize="50sp" />
<Button
android:id="@+id/btn_put"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_toRightOf="@id/btn_take"
android:text="@string/btn_put"
android:textSize="50sp" />
RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="149dp">LinearLayout>
<com.google.android.material.tabs.TabLayout
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
app:tabGravity="fill"
app:tabIndicatorGravity="bottom"
>
<com.google.android.material.tabs.TabItem
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_home"
android:text="@string/ic_home" />
<com.google.android.material.tabs.TabItem
android:id="@+id/shop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_shop"
android:text="@string/ic_shop" />
<com.google.android.material.tabs.TabItem
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/ic_user"
android:text="@string/ic_user" />
com.google.android.material.tabs.TabLayout>
LinearLayout>
请问怎么解决
应该是tablayout的使用方法不对,你查一下
这样写
TabLayout tabLayout = findViewById(R.id.tab);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
switch (tab.getPosition()){
case 0://第一个TabItem
Log.d("onTabSelected","Home");
break;
case 1://第二个TabItem
Log.d("onTabSelected","Shop");
break;
case 2://第三个TabItem
Log.d("onTabSelected","User");
break;
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
�