崩溃的android应用程序

时间:2016-09-14 20:40:19

标签: java android android-layout android-studio nullpointerexception

我正在使用android工作室我曾尝试在单击按钮时创建AlertDialog但是当我尝试运行时,应用程序崩溃。请告诉我清楚的解决方案。

Ps:按钮位于卡片视图内。

主要java:

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;

private ItemAdapter itemAdapter;

private Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
                a_builder.setCancelable(false);
                a_builder.setMessage("do you want to call!!!");
                a_builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("tel:0000000000"));
                        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            // TODO: Consider calling
                            //    ActivityCompat#requestPermissions
                            // here to request the missing permissions, and then overriding
                            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                            //                                          int[] grantResults)
                            // to handle the case where the user grants the permission. See the documentation
                            // for ActivityCompat#requestPermissions for more details.
                            return;
                        }
                        startActivity(callIntent);



                    }
                });

                a_builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog alert = a_builder.create();
                alert.setTitle("Alert !");
                alert.show();

            }
        });




        final Toolbar toolbar = (Toolbar)findViewById(R.id.MyToolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapse_toolbar);
    collapsingToolbarLayout.setTitle("Service/DPNG");


    ArrayList<Celebrity> itemList = new ArrayList<>();

    fillDummyData(itemList);

    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    itemAdapter = new ItemAdapter(itemList);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(itemAdapter);





}

private void fillDummyData(ArrayList<Celebrity> celebList) {
    Celebrity celeb1 = new Celebrity();
    celeb1.setName("Johny.D");
    celeb1.setFamousMovie("Pirates  ");
    celeb1.setProfilePhotoLocation("@drawable/contact1");
    celebList.add(celeb1);

    Celebrity celeb2 = new Celebrity();
    celeb2.setName("Arnold");
    celeb2.setFamousMovie("The Terminator");
    celeb2.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTI3MDc4NzUyMV5BMl5BanBnXkFtZTcwMTQyMTc5MQ@@._V1._SY209_CR13,0,140,209_.jpg");
    celebList.add(celeb2);

    Celebrity celeb3 = new Celebrity();
    celeb3.setName("Emma.W");
    celeb3.setFamousMovie("Harry Potter ");
    celeb3.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTQ3ODE2NTMxMV5BMl5BanBnXkFtZTgwOTIzOTQzMjE@._V1._SY209_CR14,0,140,209_.jpg");
    celebList.add(celeb3);

    Celebrity celeb4 = new Celebrity();
    celeb4.setName("Daniel");
    celeb4.setFamousMovie("Harry Potter ");
    celeb4.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTg4NTExODc3Nl5BMl5BanBnXkFtZTgwODUyMDEzMDE@._V1._SY209_CR8,0,140,209_.jpg");
    celebList.add(celeb4);

    Celebrity celeb5 = new Celebrity();
    celeb5.setName("Leonardo");
    celeb5.setFamousMovie("Inception");
    celeb5.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMjI0MTg3MzI0M15BMl5BanBnXkFtZTcwMzQyODU2Mw@@._V1._SY209_CR7,0,140,209_.jpg");
    celebList.add(celeb5);

    Celebrity celeb6 = new Celebrity();
    celeb6.setName("TomCr");
    celeb6.setFamousMovie("Top Gun");
    celeb6.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTk1MjM3NTU5M15BMl5BanBnXkFtZTcwMTMyMjAyMg@@._V1._SY209_CR9,0,140,209_.jpg");
    celebList.add(celeb6);

    Celebrity celeb7 = new Celebrity();
    celeb7.setName("Charles.C");
    celeb7.setFamousMovie("Modern Times");
    celeb7.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BNDcwMDc0ODAzOF5BMl5BanBnXkFtZTgwNTY2OTI1MDE@._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb7);

    Celebrity celeb8 = new Celebrity();
    celeb8.setName("Morgan.F");
    celeb8.setFamousMovie("The Shawshank ");
    celeb8.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTc0MDMyMzI2OF5BMl5BanBnXkFtZTcwMzM2OTk1MQ@@._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb8);

    Celebrity celeb9 = new Celebrity();
    celeb9.setName("Tom.Ha");
    celeb9.setFamousMovie("The Da Vinci Code");
    celeb9.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTQ2MjMwNDA3Nl5BMl5BanBnXkFtZTcwMTA2NDY3NQ@@._V1._SY209_CR2,0,140,209_.jpg");
    celebList.add(celeb9);

    Celebrity celeb10 = new Celebrity();
    celeb10.setName("Hugh ");
    celeb10.setFamousMovie("The Prestige");
    celeb10.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BNDExMzIzNjk3Nl5BMl5BanBnXkFtZTcwOTE4NDU5OA@@._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb10);

    Celebrity celeb11 = new Celebrity();
    celeb11.setName("Matt ");
    celeb11.setFamousMovie("The Bourne Identity");
    celeb11.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTM0NzYzNDgxMl5BMl5BanBnXkFtZTcwMDg2MTMyMw@@._V1._SY209_CR8,0,140,209_.jpg");
    celebList.add(celeb11);

    Celebrity celeb12 = new Celebrity();
    celeb12.setName("Sylvester ");
    celeb12.setFamousMovie("Rocky");
    celeb12.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTQwMTk3NDU2OV5BMl5BanBnXkFtZTcwNTA3MTI0Mw@@._V1._SY209_CR5,0,140,209_.jpg");
    celebList.add(celeb12);

    Celebrity celeb13 = new Celebrity();
    celeb13.setName("Will ");
    celeb13.setFamousMovie("Men in Black");
    celeb13.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BNTczMzk1MjU1MV5BMl5BanBnXkFtZTcwNDk2MzAyMg@@._V1._SY209_CR2,0,140,209_.jpg");
    celebList.add(celeb13);

    Celebrity celeb14 = new Celebrity();
    celeb14.setName("Steven ");
    celeb14.setFamousMovie("Saving Private ");
    celeb14.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTY1NjAzNzE1MV5BMl5BanBnXkFtZTYwNTk0ODc0._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb14);

    Celebrity celeb15 = new Celebrity();
    celeb15.setName("Al ");
    celeb15.setFamousMovie("The Godfather");
    celeb15.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTQzMzg1ODAyNl5BMl5BanBnXkFtZTYwMjAxODQ1._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb15);

    Celebrity celeb16 = new Celebrity();
    celeb16.setName("Robert");
    celeb16.setFamousMovie("The Avengers");
    celeb16.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BNzg1MTUyNDYxOF5BMl5BanBnXkFtZTgwNTQ4MTE2MjE@._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb16);

    Celebrity celeb17 = new Celebrity();
    celeb17.setName("Nat");
    celeb17.setFamousMovie("V for Vendetta");
    celeb17.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTQ3ODE3Mjg1NV5BMl5BanBnXkFtZTcwNzA4ODcxNA@@._V1._SY209_CR8,0,140,209_.jpg");
    celebList.add(celeb17);

    Celebrity celeb18 = new Celebrity();
    celeb18.setName("Dwayne");
    celeb18.setFamousMovie("WWE Raw");
    celeb18.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTkyNDQ3NzAxM15BMl5BanBnXkFtZTgwODIwMTQ0NTE@._V1._SX140_CR0,0,140,209_.jpg");
    celebList.add(celeb18);

    Celebrity celeb19 = new Celebrity();
    celeb19.setName("Jackie ");
    celeb19.setFamousMovie("Jackie Chan ");
    celeb19.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTk4MDM0MDUzM15BMl5BanBnXkFtZTcwOTI4MzU1Mw@@._V1._SY209_CR5,0,140,209_.jpg");
    celebList.add(celeb19);

    Celebrity celeb20 = new Celebrity();
    celeb20.setName("Scarlet");
    celeb20.setFamousMovie("Lost in ");
    celeb20.setProfilePhotoLocation("http://ia.media-imdb.com/images/M/MV5BMTM3OTUwMDYwNl5BMl5BanBnXkFtZTcwNTUyNzc3Nw@@._V1._SY209_CR16,0,140,209_.jpg");
    celebList.add(celeb20);



}

}

logcat:

09-14 21:18:04.476 26368-26368/com.example.apollo.bbpp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.apollo.bbpp/com.example.apollo.bbpp.MainActivity}: java.lang.NullPointerException
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
                                                                         at android.app.ActivityThread.access$600(ActivityThread.java:138)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                         at android.os.Looper.loop(Looper.java:137)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:4929)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:511)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                      Caused by: java.lang.NullPointerException
                                                                         at com.example.apollo.bbpp.MainActivity.onCreate(MainActivity.java:48)
                                                                         at android.app.Activity.performCreate(Activity.java:5254)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2038)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099) 
                                                                         at android.app.ActivityThread.access$600(ActivityThread.java:138) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                         at android.os.Looper.loop(Looper.java:137) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:4929) 
                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798) 

1 个答案:

答案 0 :(得分:0)

    public void onClick(View v) {
        AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this);
        a_builder.setCancelable(false);
        a_builder.setTitle("Alert !");
        a_builder.setMessage("do you want to call!!!");
        a_builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //handle the permission at start.
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    //permission not granted, ask for permission
                    return;
                }

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:0000000000"));
                startActivity(callIntent);
            }
        });

        a_builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        AlertDialog alert = a_builder.create();
        alert.show();

    }
});