java - 尝试调用虚方法 FAB 按钮
问题描述
最近在我的项目中,我想在主活动中添加浮动操作按钮按钮功能,但是在我将功能添加到主活动后,应用程序崩溃并出现错误。
日志猫
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ainuralizar.firebaseproject, PID: 13371
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ainuralizar.firebaseproject/com.ainuralizar.firebaseproject.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.floatingactionbutton.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3333)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3477)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.material.floatingactionbutton.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.ainuralizar.firebaseproject.MainActivity.onCreate(MainActivity.java:115)
at android.app.Activity.performCreate(Activity.java:7990)
at android.app.Activity.performCreate(Activity.java:7979)
这是我的 FAB 部分
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
Adapter adapter;
List<Model> models;
Integer[] colors = null;
ArgbEvaluator argbEvaluator = new ArgbEvaluator();
DatabaseReference mRef;
FirebaseDatabase mFirebaseDatabase;
RecyclerView mRecyclerView;
private FloatingActionButton fab_main, fab1_mail, fab2_share;
private Animation fab_open, fab_close, fab_clock, fab_anticlock;
TextView textview_mail, textview_share;
Boolean isOpen = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mRecyclerView = findViewById(R.id.recyclerView);
// innerDataModelsMenu1ForAdapter = new ArrayList<>();
// innerDataModelsMenu2ForAdapter = new ArrayList<>();
models = new ArrayList<>();
models.add(new Model(R.drawable.sample1, "Menu1", "THIS IS MENU 1"));
models.add(new Model(R.drawable.sample2, "Menu2", " THIS IS MENU 2"));
models.add(new Model(R.drawable.sample3, "Menu3", " THIS IS MENU 3"));
models.add(new Model(R.drawable.sample4, "Menu4", " THIS IS MENU 4"));
models.add(new Model(R.drawable.sample5, "Menu5", " THIS IS MENU 5"));
models.add(new Model(R.drawable.sample6, "Menu6", " THIS IS MENU 6"));
models.add(new Model(R.drawable.sample7, "Menu7", " THIS IS MENU 6"));
adapter = new Adapter(models, this);
viewPager = findViewById(R.id.viewPager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130, 0, 130, 0);
Integer[] colors_temp = {getResources().getColor(R.color.color1), getResources().getColor(R.color.color2), getResources().getColor(R.color.color3), getResources().getColor(R.color.color4), getResources().getColor(R.color.color5), getResources().getColor(R.color.color6), getResources().getColor(R.color.color7)};
colors = colors_temp;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (position < (adapter.getCount() - 1) && position < (colors.length - 1)) {
viewPager.setBackgroundColor(
(Integer) argbEvaluator.evaluate(positionOffset, colors[position], colors[position + 1])
);
} else {
viewPager.setBackgroundColor(colors[colors.length - 1]);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
fab_main = findViewById(R.id.fab);
fab1_mail = findViewById(R.id.fab1);
fab2_share = findViewById(R.id.fab2);
fab_close = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
fab_open = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
fab_clock = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_rotate_clock);
fab_anticlock = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_rotate_anticlock);
textview_mail = (TextView) findViewById(R.id.textview_mail);
textview_share = (TextView) findViewById(R.id.textview_share);
fab_main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isOpen) {
textview_mail.setVisibility(View.INVISIBLE);
textview_share.setVisibility(View.INVISIBLE);
fab2_share.startAnimation(fab_close);
fab1_mail.startAnimation(fab_close);
fab_main.startAnimation(fab_anticlock);
fab2_share.setClickable(false);
fab1_mail.setClickable(false);
isOpen = false;
} else {
textview_mail.setVisibility(View.VISIBLE);
textview_share.setVisibility(View.VISIBLE);
fab2_share.startAnimation(fab_open);
fab1_mail.startAnimation(fab_open);
fab_main.startAnimation(fab_clock);
fab2_share.setClickable(true);
fab1_mail.setClickable(true);
isOpen = true;
}
}
});
fab2_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Share", Toast.LENGTH_SHORT).show();
}
});
fab1_mail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Email", Toast.LENGTH_SHORT).show();
}
});
}
这是我的工厂 XML
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/textview_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="186dp"
android:layout_marginEnd="88dp"
android:background="#212121"
android:padding="8dp"
android:text="Share"
android:textColor="#ffffff"
android:visibility="invisible" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="176dp"
android:layout_marginEnd="24dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:elevation="16dp"
android:focusable="true"
android:visibility="invisible"
app:backgroundTint="#ffffff"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/icon_share" />
<TextView
android:id="@+id/textview_mail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="116dp"
android:layout_marginEnd="88dp"
android:background="#212121"
android:padding="8dp"
android:text="Email"
android:textColor="#ffffff"
android:visibility="invisible" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="106dp"
android:layout_marginEnd="24dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:elevation="16dp"
android:focusable="true"
android:visibility="invisible"
app:backgroundTint="#ffffff"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/icon_email" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="32dp"
android:layout_marginEnd="24dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:elevation="16dp"
android:focusable="true"
android:visibility="visible"
app:backgroundTint="#ffffff"
app:pressedTranslationZ="12dp"
app:srcCompat="@drawable/icon_add" />
</LinearLayout>
我看过几个相关的帖子,但还没有任何效果,
对不起,我只是一个初学者,我应该改变哪个部分?
解决方案
推荐阅读
- java - Android中的Tensorflow:java.lang.illegalArgumentsException
- sql-server - 聚合可能不会出现在 UPDATE 语句的集合列表中。变通逻辑?
- mongodb - 从clickhouse中的外部字典中检索所有记录
- java - 解析带前缀的逗号分隔字符串
- ios - 如何使用受密码保护的电子邮件通过电子邮件发送 zip 文件并在目标 c 中通过电子邮件将其解压缩
- node.js - 在 koa2 应用中启用 Http2Stream
- vba - VBA 在 Win7x64 中单击 IE11 另存为对话框
- java - @preauthorize spring boot 显示访问权限错误
- python - 从 protobuf 动态访问 oneof 值
- php - 添加记录json传递两个空数据