有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓在来自不同Java类文件的活动中祝酒

我有2个.java文件:AnswerActivity.java

public class AnswerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);

        new JSONTask().execute("https://example.com/api.php");
    }
}

JSONTask.java

public class JSONTask extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {

    //--- some code cut here ---//

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        Intent i = new Intent(this, AnswerActivity.class);
        Toast.makeText(i, result, Toast.LENGTH_LONG).show();
    }
}

我的最后两行总是给我错误:

    Intent i = new Intent(this, AnswerActivity.class);
    Toast.makeText(i, result, Toast.LENGTH_LONG).show();

我想以祝酒词的形式将result显示到AnswerActivity中。但是为什么我总是收到这个错误消息呢

Error:(68, 20) error: no suitable constructor found for Intent(JSONTask,Class<AnswerActivity>)
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; JSONTask cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; JSONTask cannot be converted to Context)

Error:(69, 14) error: no suitable method found for makeText(Intent,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; Intent cannot be converted to Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; Intent cannot be converted to Context)

我错过了什么?多谢各位


共 (5) 个答案

  1. # 1 楼答案

    删除以下内容:

        Intent i = new Intent(this, AnswerActivity.class);
        Toast.makeText(i, result, Toast.LENGTH_LONG).show();
    

    有了这个:

    AsyncTask类中传递Context

    public class JSONTask extends AsyncTask<String, String, String> {
    private Context context;
    //in constructor:
    public JSONTask(Context context){
        this.context=context;
    }
    
    @Override
    protected String doInBackground(String... params) {
    
    //--- some code cut here ---//
    
    }
    
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    
    
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    }
    }
    

    Activity中使用:

    new JSONTask(AnswerActivity.this).execute("https://example.com/api.php");
    
  2. # 2 楼答案

    您需要一个上下文来显示Toast。通常是ActivityFragment有上下文。但是因为你的AsyncTask不是你的AnswerActivity的内部类,所以你没有对上下文的引用。 所以你可以在构造函数中传递Activity并使用它

    public class JSONTask extends AsyncTask<String, String, String> {
    
        private Context context;
    
        public JSONTask(Context context){
          this.context = context;
        }
    
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        }
    }
    

    和它一起使用

    new JSONTask(this)... // and so on
    

    这样做的最大优点是,您不需要保留对Activity的引用,只需传递Context,因为活动确实扩展了上下文

  3. # 3 楼答案

    您的JSONTask应该是

    public class JSONTask extends AsyncTask<String, String, String> {
    
        Context mContext;
    
        public JSONTask(Context mContext) {
            this.mContext = mContext;
        }
    
        @Override
        protected String doInBackground(String... params) {
            //--- some code cut here ---//
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Toast.makeText(mContext, result, Toast.LENGTH_LONG).show();
        }
    }
    

    初始化

     new JSONTask(this).execute("https://example.com/api.php");
    
  4. # 4 楼答案

    如果您没有在其他任何地方使用JSONTask类,那么可以在AnswerActivity中将其定义为inner class。如果你这么做了,那么这段代码应该会起作用

    Toast.makeText(AnswerActivity.this, result, Toast.LENGTH_LONG).show();
    

    否则,您必须将应用程序上下文传递给JSONTask,并在Toast中使用它

    public class JSONTask extends AsyncTask<String, String, String> {
    
        private Context appContext;
    
        public JSONTask(Context context){
            this.appContext= context;
        }
    
        @Override
        protected String doInBackground(String... params) {
    
        //--- some code cut here ---//
    
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);    
            Toast.makeText(appContext, result, Toast.LENGTH_LONG).show();
        }
    }
    
  5. # 5 楼答案

    更好的方法是在类中创建构造函数

    Context context;
    
    public void init(Context ctx){
           this.context = ctx;
    }
    

    然后你可以制作吐司

    Toast.makeText(this.context, result, Toast.LENGTH_LONG).show();