2016-07-19 11 views
-3

私は2つのクラスを持っています(MainActivity &ダイアログ) 私はDialogsクラス(コンテキストとして)をMainAcitivityに呼び出そうとしています。私は クラス他のクラスからのコンテキスト

 Dialogs WorkOnDialog = (Dialogs)context; 
     WorkOnDialog.WorkOnSavedDialog(); 

この

を試してみました

public class Dialogs extends AppCompatActivity { 

public Context context = this; 

public void WorkOnSavedDialog() { 
    new AlertDialog.Builder(context) 
      .setTitle("Work On File Saved") 
      .setMessage("[Displaying for Test] Work On File Saved") 
      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // continue with delete 
       } 
      }) 
      .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // do nothing 
       } 
      }) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .show(); 
} 

public void WorkOffSavedDialog() { 
    new AlertDialog.Builder(context) 
      .setTitle("Work Off File Saved") 
      .setMessage("[Displaying for Test] Work Off File Saved") 
      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // continue with delete 
       } 
      }) 
      .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // do nothing 
       } 
      }) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .show(); 
} 


} 

私はこれを呼び出すことができません、なぜ誰も私に説明することができ、「これは私のダイアログクラスである私のMainAcitivty

public class MainActivity extends AppCompatActivity { 



    Context context = this; 
    boolean WorkOn = false; 
    boolean WorkOff = false; 
    Dialogs WorkOnSaveDialog; 
// Dialogs WorkOnSaveDialog = (Dialogs) context; 
// Dialogs WorkOffSaveDialog = (Dialogs)context; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 


     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


    } 


    //Press Work On Button 
    public void WorkOnClicked(View v) { 

     //Creates ImageButton var for ActionListener 
     ImageButton WorkOnClicked = (ImageButton) findViewById(R.id.WorkOn); 

     WorkOnClicked.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // Gets & Sets Date, Filename, Data Content 
       // Creates Output Stream to save to file 

       String nowDate = DateFormat.getDateTimeInstance().format(new Date()); 

       String filename = "WorkOnFile"; 
       String string = nowDate + " Work On"; 
       FileOutputStream outputStream; 

       File file = new File(Environment.getDataDirectory(), filename); 


       System.out.println("Work On Button CLicked"); 

       try { 
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 
        outputStream.write(string.getBytes()); 
        outputStream.close(); 
        System.out.println("!Wrote " + string + " !"); 
        System.out.println("Wrote at " + file); 
        WorkOn = true; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Dialogs WorkOnDialog = (Dialogs)context; 
       WorkOnDialog.WorkOnSavedDialog(); 


      } 



     }); 
    } 

    //Press WorkOff Button 
    public void WorkOffClicked(View v) { 

     //Creates ImageButton var for ActionListener 

     ImageButton WorkOffClicked = (ImageButton) findViewById(R.id.WorkOff); 

     WorkOffClicked.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // Gets & Sets Date, Filename, Data Content 
       // Creates Output Stream to save to file 
       String nowDate = DateFormat.getDateTimeInstance().format(new Date()); 

       String filename = "WorkOffFile"; 
       String string = nowDate + " Work Off"; 
       FileOutputStream outputStream; 

       File file = new File(Environment.getDataDirectory(), filename); 


       System.out.println(" Work Off Button CLicked"); 

       try { 
        outputStream = openFileOutput(filename, Context.MODE_PRIVATE); 
        outputStream.write(string.getBytes()); 
        outputStream.close(); 
        System.out.println("!Wrote " + string + " !"); 
        System.out.println("Wrote at " + file); 
        WorkOn=false; 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

//    WorkOffSaveDialog.WorkOffSavedDialog(); 

      } 

     }); 

ですメインメソッド内のクラス "ダイアログ コンテキストを使用してメソッドを呼び出す方法を検索しましたが、見つかったものはうまくいかず、正確に何がうまくいかず、なぜですか?あなたのダイアログクラスにコンストラクタを作成し、このコンストラクタにMainActivityコンテキストを渡す必要があり

答えて

0

public class Dialogs extends AppCompatActivity { 

public Context context; 

public Dialogs(Context context) { 
this.context = context; 
} 

public void WorkOnSavedDialog() { 
    new AlertDialog.Builder(context) 
      .setTitle("Work On File Saved") 
      .setMessage("[Displaying for Test] Work On File Saved") 
      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // continue with delete 
       } 
      }) 
      .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // do nothing 
       } 
      }) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .show(); 
} 

public void WorkOffSavedDialog() { 
    new AlertDialog.Builder(context) 
      .setTitle("Work Off File Saved") 
      .setMessage("[Displaying for Test] Work Off File Saved") 
      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // continue with delete 
       } 
      }) 
      .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // do nothing 
       } 
      }) 
      .setIcon(android.R.drawable.ic_dialog_alert) 
      .show(); 
} 
} 
+0

はあなたの助けのためにどうもありがとうございました:) – deathsonic