-5

我正在使用android studio我嘗試創建一個AlertDialog單擊按鈕時,但是當我嘗試運行時,應用程序崩潰。請給我清楚的解決方案。崩潰android應用程序

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/[email protected]@._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/[email protected]_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/[email protected]_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/[email protected]@._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/[email protected]@._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/[email protected]_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/[email protected]@._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/[email protected]@._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/[email protected]@._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/[email protected]@._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/[email protected]@._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/[email protected]@._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/[email protected]_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/[email protected]@._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/[email protected]_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/[email protected]@._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/[email protected]@._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)  
+0

你的錯誤是一個空指針。這意味着你想要參考的東西不在那裏。您需要查看logcat中的行號,並確定導致錯誤的行。 – durbnpoisn

+0

logcat中的行號是:at com.example.apollo.bbpp.MainActivity.onCreate(MainActivity.java:48),start in。 b1.setOnClickListener(new View.OnClickListener(){ –

+0

請發佈您的activity_main.xml佈局文件。您是否在佈局中定義了Button按鈕1? –

回答

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(); 

    } 
});