我有两个类(MainActivity& Dialogs) 我试图将Dialogs类(作为上下文)称为MainAcitivity;
我试过了
Dialogs WorkOnDialog = (Dialogs)context;
WorkOnDialog.WorkOnSavedDialog();
这是我的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();
}
});
这是我的Dialogs类
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();
}
}
任何人都可以向我解释为什么我无法称呼这个" Dialogs"主要方法内的类? 我已经搜索过使用上下文调用方法,我找到的所有内容都没有用,究竟出了什么问题,为什么?
答案 0 :(得分:0)
您需要为Dialog类创建构造函数并将MainActivity上下文传递给此构造函数:
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();
}
}