浮动按钮上的空指针

时间:2017-06-28 04:51:12

标签: android nullpointerexception floating-action-button

我为电话拨号创建了浮动操作按钮。试图运行应用程序但最终在我的晶圆厂电话中出错。 我不知道浮动动作按钮出了什么问题。 你能告诉我如何解决它吗?

这是我的fab代码

    FloatingActionButton fabshare = (FloatingActionButton) findViewById(fab);
                FloatingActionButton fabcall = (FloatingActionButton) findViewById(fab);

                fabshare.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Intent i = new Intent(Intent.ACTION_SEND);
                        i.setType("text/plain");
                        String shareSubject = String.valueOf(dataLaundry.getNama());
                        String shareBody = String.valueOf(dataLaundry.getAlamat());
                        i.putExtra(Intent.EXTRA_SUBJECT, "Laundrize - " + shareSubject);
                        i.putExtra(Intent.EXTRA_TEXT, shareSubject + "\n" + shareBody + "\n\n" +
                                    "http://maps.google.com/?q="
                        + alamat.getText().toString().toLowerCase().replaceAll(" ","+"));
                        startActivity(Intent.createChooser(i, "Share via"));


                    }
                });
                fabcall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent callIntent = new Intent (Intent.ACTION_DIAL, Uri.parse("tel:"+ noTelp));
                        startActivity(callIntent);
                                                }
                                            });\

这里是错误

    FATAL EXCEPTION: main
                                                                           Process: com.example.rizkafs.laundrize, PID: 25885
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rizkafs.laundrize/com.example.rizkafs.laundrize.DetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2660)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730)
                                                                               at android.app.ActivityThread.access$800(ActivityThread.java:185)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                               at android.os.Looper.loop(Looper.java:194)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5847)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:372)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.rizkafs.laundrize.DetailsActivity.onCreate(DetailsActivity.java:46)
                                                                               at android.app.Activity.performCreate(Activity.java:6117)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730) 
                                                                               at android.app.ActivityThread.access$800(ActivityThread.java:185) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                               at android.os.Looper.loop(Looper.java:194) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5847) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 

1 个答案:

答案 0 :(得分:0)

在活动中定义两个Fab图标的ID。

 FloatingActionButton fabshare = (FloatingActionButton) findViewById(R.id.fabshare);
 FloatingActionButton fabcall = (FloatingActionButton) findViewById(R.id.fabcall);