如果我不想在警告对话框中添加按钮,我可以添加警告对话框。但是,当我想添加一个警告对话框,其中包含2个按钮,其中包含onPostExecute'当我输入此代码时,它显示红线:
Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
红线显示的错误:
无法解析方法' makeText(匿名android.content.DialogInterface.OnClickListener,java.lang.String,int)'
我按照此链接中的教程:https://www.youtube.com/watch?v=nW_nvmmxURc
这是我执行帖子中的代码:
@Override
protected void onPostExecute(String result) {
if (type.equals("cleck")){
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("PUV Checker");
alertDialog.setMessage("Sample");
}else {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Comment");
}
alertDialog.setMessage(result);
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
注意:我是一个完全的初学者,只是在不同的教程中编写想法
编辑:添加了整个代码
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by Julian on 7/21/2017.
*/
public class BackgroundWorker extends AsyncTask<String,Void, String> {
String type;
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context = ctx;
}
@Override
protected String doInBackground(String... params) {
type = params[0];
String register_url = "http://10.0.2.2/insert_comment";
String retrieve_id = "http://10.0.2.2/rizal3.php";
String report_id = "http://10.0.2.2/rizal4.php";
if (type.equals("button")) {
try {
String id = params[1];
String comment = params[2];
String rating = params[3];
URL url = new URL(register_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("id", "UTF-8") + "=" + URLEncoder.encode(id, "UTF-8") + "&"
+ URLEncoder.encode("comment", "UTF-8") + "=" + URLEncoder.encode(comment, "UTF-8") + "&"
+ URLEncoder.encode("rating", "UTF-8") + "=" + URLEncoder.encode(rating, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if(type.equals("cleck")) {
try {
String id_ret = params[1];
URL url = new URL(retrieve_id);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("PLATE_NUM", "UTF-8") + "=" + URLEncoder.encode(id_ret, "UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if(type.equals("report")){
try {
String id_ret = params[1];
String puv_type = params[2];
String report = params[3];
URL url = new URL(report_id);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("PLATE_NUM","UTF-8")+"="+URLEncoder.encode(id_ret,"UTF-8") + "&"
+ URLEncoder.encode("PUV_TYPE", "UTF-8") + "=" + URLEncoder.encode(puv_type, "UTF-8") + "&"
+ URLEncoder.encode("content", "UTF-8") + "=" + URLEncoder.encode(report, "UTF-8");;
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
while((line = bufferedReader.readLine())!= null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
}
@Override
protected void onPostExecute(String result) {
if (type.equals("cleck")){
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("PUV Checker");
alertDialog.setMessage("Sample");
}else {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Comment");
}
alertDialog.setMessage(result);
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
我打电话给后台工作人员:
public void Button(View view) {
String puv_plate = report_plate.getText().toString();
String puv_type = report_type.getText().toString();
String content = report_content.getText().toString();
String type="report";
BackgroundWorker backgroundWorker = new BackgroundWorker(Report.this);
backgroundWorker.execute(type, puv_plate,puv_type,content);
}
编辑:
使用此行时出现的错误
Toast.makeText(BackgroundWorker.this, "Yes", Toast.LENGTH_SHORT).show();
无法解析方法&#39; makeText(rjj.manilapuvtravelcompanion.BackgroundWorker,java.lang.String,int)&#39;
编辑:
Report.java中的整个代码(我称之为BackgroundWorker)
package rjj.manilapuvtravelcompanion;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Report extends AppCompatActivity {
EditText report_plate, report_type, report_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
report_plate =(EditText)findViewById(R.id.report_plate_num);
report_type = (EditText)findViewById(R.id.report_puv_type);
report_content =(EditText)findViewById(R.id.report_content);
}
public void Button(View view) {
String puv_plate = report_plate.getText().toString();
String puv_type = report_type.getText().toString();
String content = report_content.getText().toString();
String type="report";
BackgroundWorker backgroundWorker = new BackgroundWorker(Report.this);
backgroundWorker.execute(type, puv_plate,puv_type,content);
}
}
答案 0 :(得分:0)
public static Toast makeText(Context context,CharSequence text,int 持续时间)
您应该传递 context 。
有关应用程序环境的全局信息的接口。这个 是一个抽象类,其实现由Android提供 系统。
Toast.makeText(context, "Yes", Toast.LENGTH_SHORT).show();
代码
alertDialog = new AlertDialog.Builder(context).create();
if (type.equals("cleck"))
{
alertDialog.setTitle("PUV Checker");
alertDialog.setMessage("Sample");
}
else
{
alertDialog.setTitle("Comment");
alertDialog.setMessage(result);
}
alertDialog.setButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(context, "Yes", Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
答案 1 :(得分:0)
您提供DialogInterface.OnCLickListener引用而不是上下文。 这样做
Toast.makeText(getApplicationContext(), "Yes", Toast.LENGTH_SHORT).show();
答案 2 :(得分:0)
Toast.makeText(MainActivity.this, "Yes", Toast.LENGTH_SHORT).show();
使用Activity。如果您的代码处于活动状态,则此功能正常。
答案 3 :(得分:0)
尝试使用下面而不是传递这个`
Toast.makeText(<YourMainActivity>.this,`...
您也可以使用getApplicationContext()或getBaseContext(),但不建议使用它们,除非需要应用程序上下文。