Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我应该在Android环境中使用什么?_Java_Android_Android Alertdialog_Android Context - Fatal编程技术网

Java 我应该在Android环境中使用什么?

Java 我应该在Android环境中使用什么?,java,android,android-alertdialog,android-context,Java,Android,Android Alertdialog,Android Context,我试图使用“Android配方”中的代码: …但我不知道应该用什么来代替“上下文”。我尝试了.java文件的类、立即类和“this”,但都没有编译 在更多的上下文中,代码是: public class SQLiteActivity extends ActionBarActivity { private FetchAndPopTask _fetchAndPopTask; . . . private class FetchAndPopTask extends AsyncTask<Stri

我试图使用“Android配方”中的代码:

…但我不知道应该用什么来代替“上下文”。我尝试了.java文件的类、立即类和“this”,但都没有编译

在更多的上下文中,代码是:

public class SQLiteActivity extends ActionBarActivity {

private FetchAndPopTask _fetchAndPopTask;

. . .

private class FetchAndPopTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        . . .
        try {
            . . .
        } catch (Exception e) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this); // <= "context"...?
            builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
            builder.setMessage(e.getMessage());
            builder.setPositiveButton("OK", null);
            builder.create().show();
            return result;
    }
…但没有人编译;那么“上下文”需要在这里是什么呢?

AlertDialog.Builder(SQLiteActivity.this)
应该可以工作

但是看看

编辑 抱歉,没有注意到您试图在非UI线程中显示它。请将其放置在构造函数或
onPreExecute()
/
onPostExecute()
方法中

AlertDialog.Builder(SQLiteActivity.this)
可能会起作用

但是看看

编辑
抱歉,没有注意到您试图在非UI线程中显示它。请将其放置在构造函数中或
onPreExecute()
/
onPostExecute()
方法中

您必须将
活动
传递给此构造函数

您必须将其添加到
FetchAndPopTask
类中:

private SQLiteActivity context;

public FetchAndPopTask(SQLiteActivity context) {
    this.context = context;
}
然后,在
SQLiteActivity
类中,您必须使用
this
关键字传递此上下文(因为您在活动中,它引用它):


您必须将
活动
传递给此构造函数

您必须将其添加到
FetchAndPopTask
类中:

private SQLiteActivity context;

public FetchAndPopTask(SQLiteActivity context) {
    this.context = context;
}
然后,在
SQLiteActivity
类中,您必须使用
this
关键字传递此上下文(因为您在活动中,它引用它):

this
表示您正在获取
FetchAndPopTask
的指针/引用,因此不要使用
this
而是通过调用
SQLiteActivity来使用
SQLiteActivity
的指针/引用。this


this
表示您正在获取
FetchAndPopTask
的指针/引用,因此,不要使用
this
而是通过调用
SQLiteActivity来使用
SQLiteActivity
的指针/引用。这将在后台线程上运行。您不能在非UI线程上触摸UI。这包括显示对话框。从
doInBackground()
中删除
AlertDialog
。您可以将其放入在UI线程上运行的
onPostExecute()
中。在那里,您可以使用
YourActivityName。this
引用外部类
this
用作
上下文

doInBackground()
将在后台线程上运行。您不能在非UI线程上触摸UI。这包括显示对话框。从
doInBackground()
中删除
AlertDialog
。您可以将其放入在UI线程上运行的
onPostExecute()
中。在那里,您可以使用
YourActivityName。this
引用外部类
this
用作
上下文

定义类的构造函数并将上下文传递到那里

private class FetchAndPopTask extends AsyncTask<String, String, String> {

private Context mContext;

public FetchAndPopTask(Context context){
    mContext = context;
}

@Override
protected String doInBackground(String... params) {
    . . .
    try {
        . . .
    } catch (Exception e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext); // <= use the variable here
        builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
        builder.setMessage(e.getMessage());
        builder.setPositiveButton("OK", null);
        builder.create().show();
        return result;
}
私有类FetchAndPopTask扩展了AsyncTask{
私有上下文;
公共FetchAndPopTask(上下文){
mContext=上下文;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
. . .
试一试{
. . .
}捕获(例外e){

AlertDialog.Builder=新建AlertDialog.Builder(mContext);//为类定义构造函数并将上下文传递给该类

private class FetchAndPopTask extends AsyncTask<String, String, String> {

private Context mContext;

public FetchAndPopTask(Context context){
    mContext = context;
}

@Override
protected String doInBackground(String... params) {
    . . .
    try {
        . . .
    } catch (Exception e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext); // <= use the variable here
        builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
        builder.setMessage(e.getMessage());
        builder.setPositiveButton("OK", null);
        builder.create().show();
        return result;
}
私有类FetchAndPopTask扩展了AsyncTask{
私有上下文;
公共FetchAndPopTask(上下文){
mContext=上下文;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
. . .
试一试{
. . .
}捕获(例外e){

AlertDialog.Builder=新建AlertDialog.Builder(mContext);//您可以看到异步任务类是非静态的,因此可以使用'SQLiteActivity.this'来shorter@SamN-a我不知道。事实上它更短更方便。谢谢;)不会说它更方便,因为它在重构时可能会导致一些额外的工作(例如,当您将asynctask移出此类时)。但是它较短;)您可以看到异步任务类是非静态的,因此可以使用“SQLiteActivity.this”来shorter@SamN-a我不知道这一点。事实上它更短更方便。谢谢;)不会说它更方便,因为它在重构时可能会导致一些额外的工作(例如,当您将asynctask从这个类中移出时).但是它比较短;)
new AlertDialog.Builder(this);
private class FetchAndPopTask extends AsyncTask<String, String, String> {

private Context mContext;

public FetchAndPopTask(Context context){
    mContext = context;
}

@Override
protected String doInBackground(String... params) {
    . . .
    try {
        . . .
    } catch (Exception e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext); // <= use the variable here
        builder.setTitle("If I go blind, I'll use a Service Platypus (instead of a Service Dog)");
        builder.setMessage(e.getMessage());
        builder.setPositiveButton("OK", null);
        builder.create().show();
        return result;
}