getActivity() where it is defined?

前端 未结 11 1958
温柔的废话
温柔的废话 2020-12-13 10:19

I\'m very new to android and I\'m following this example.

The code says we need to do these steps to get an dialog box:

AlertDialog.Builder builder =         


        
相关标签:
11条回答
  • 2020-12-13 10:34

    getActivity when you use then no need to put new... such as

    PendingIntent pi=new PendingIntent.getActivity(this,0,intent,0); //is wrong
    

    user it as :

    PendingIntent pi=PendingIntent.getActivity(this,0,intent,0); //is Right code
    

    here we remove new that provide new allocation but here it provide its allocation via getActivity in with in Activity(this).

    0 讨论(0)
  • 2020-12-13 10:37

    getActivity() is implemented in the Fragment class.

    See http://developer.android.com/reference/android/app/Fragment.html

    0 讨论(0)
  • 2020-12-13 10:37

    // 1. Instantiate an AlertDialog.Builder with its constructor

    AlertDialog.Builder builder = new AlertDialog.Builder(yourActivityName.this);
    

    // 2. Chain together various setter methods to set the dialog characteristics

    builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);
    

    // 3. Get the AlertDialog from create()

    AlertDialog dialog = builder.create();
    

    // 4. Show the AlertDialog

    dialog.show();
    
    0 讨论(0)
  • 2020-12-13 10:39

    new AlertDialog.Builder() needs Context as input parameter. So try like

    AlertDialog.Builder builder = new AlertDialog.Builder(yourActivityName.this);
    
    0 讨论(0)
  • 2020-12-13 10:41

    The constructor AlertDialog.Builder expects a Context parameter. Context is accessible from Activity, Service etc, since they all extend Context, and can be passed as this.

    The method getActivity() is declared as others have mentiond in the Fragment class.

    0 讨论(0)
提交回复
热议问题