如何从另一个类调用dialog()方法?

时间:2012-09-29 19:17:02

标签: android android-dialog

我一直试图从另一个类调用dialog()方法。我有两个类:第一个是我的主,第二个是dialog()方法。我一直试图调用一个方法并打印log.d,这是workinkg!但是,当我尝试调用dialog()方法对话框时,它会给我一个强制关闭。

ahh.. thank you for your answer.
I've been trying
>>>1. Second sd = new Second();
                    >>>         sd.dialog(); >>>>>> force close
                 2. >>>((class second) context).dialog(); >>>>>>>> force close too.

this code firstclass

>>>public void onSlideMenuItemClick(int itemId) {
>>>     switch(itemId) {        
>>>     case R.id.recomment_mode:
>>>         Header_text = (TextView)findViewById(R.id.txtHeading);
>>>         Header_text.setText(R.string.recomment_mode);
>>>         Toast.makeText(this,R.string.recomment_mode, >>>Toast.LENGTH_SHORT).show();
>>>         reg sd = new reg();
   >>>                     sd.dialog(); >>>>>>>>>>>> error
>>>         break;
>>>     case R.id.tracking_mode:
>>>         Header_text = (TextView)findViewById(R.id.txtHeading);
>>>         Header_text.setText(R.string.tracking_mode);
>>>         Toast.makeText(this,R.string.tracking_mode, >>>Toast.LENGTH_SHORT).show();
>>>         ShowDialog();
>>>         break;
>>>     case MYITEMID:
>>>         Toast.makeText(this, "Dynamically added item selected", >>>Toast.LENGTH_SHORT).show();
>>>         break;
>>>     }
>>>     
>>> }
>>>
>>>
and this code second class

>>> @Override
   >>> protected void onCreate(Bundle savedInstanceState) {
    >>> 
>>>        super.onCreate(savedInstanceState);
   >>>     setContentView(R.layout.reg);
>>>
   >>> }
    >>>protected void dialogInput(){
    >>> LayoutInflater li = LayoutInflater.from(context);
    >>> 
    >>> View promptsView = li.inflate(R.layout.dialog, null);
>>>
>>>     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
>>>     
>>>     alertDialogBuilder.setView(promptsView);
>>>     
>>>     final EditText userInput = (EditText) >>>promptsView.findViewById(R.id.editTextDialogUserInput);

>>>     alertDialogBuilder.setCancelable(false).setPositiveButton("OK",new >>>DialogInterface.OnClickListener() {

>>>         public void onClick(DialogInterface dialog, int which) {
>>>             Intent go = new >>>Intent(getApplicationContext(),maprec.class);
>>>             startActivity(go);

>>>             
>>>         }
>>>     })
>>>     .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
>>>
>>>         public void onClick(DialogInterface dialog, int which) {
>>>             // TODO Auto-generated method stub
>>>             dialog.cancel();
>>>         }
>>>     });
>>>     
>>>     AlertDialog alertDialog = alertDialogBuilder.create();
>>>     
>>>     alertDialog.show();
>>> }
}>>>
>>>
>>>
>>>
But!!! when change dialog() to
>>>public void dialog(){
>>>Log.D(TAG,"Hello world"); >>>>>>>>>>>> it's working 
>>>}


T-T I don't understand when new method
>>>public void test(){
>>>Log.d (Tag,"string");
>>>}

And use rd.test()
        rd.dialog()

in function onSlideMenuItemClick(int itemId).Of course,rd.test() it's working but not rd.dialog() 

am so sadddddd T-T

0 个答案:

没有答案