我在尝试向服务器发送数据时遇到了问题。它说泄漏的窗户最初是在这里添加的。这是我的代码
public class Feedback extends AppCompatActivity {
Spinner topic;
EditText subject, feedback;
Button btnsend;
List<String> listTopic;
ArrayAdapter<String> listarray;
String topik, subj, nup, feed;
private ProgressDialog pDialog;
String feed_url = "http://ds.bki.co.id:7777/ds/android/feedback.php";
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback);
builder = new AlertDialog.Builder(Feedback.this);
listTopic = new ArrayList<String>();
listTopic.add("Interface");
listTopic.add("Menu");
listTopic.add("Features Request");
listTopic.add("Bugs");
listTopic.add("Others");
topic = (Spinner) findViewById(R.id.spinner);
listarray = new ArrayAdapter<String>(getApplicationContext(), simple_dropdown_item_1line, listTopic);
listarray.setDropDownViewResource(simple_dropdown_item_1line);
topic.setAdapter(listarray);
topic.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
topik = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
subject = (EditText) findViewById(R.id.subJect);
feedback =(EditText) findViewById(R.id.feedBack);
btnsend =(Button) findViewById(R.id.btnSend);
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subj = subject.getText().toString();
feed = feedback.getText().toString();
nup = SaveSharedPreference.getNup(getApplicationContext());
Log.d("a", subj);
Log.d("b", feed);
Log.d("c", nup);
Log.d("d", topik);
new SendFB().execute();
}
});
}
private class SendFB extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Feedback.this);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
if (subj.equals("")||feed.equals("")){
builder.setTitle("Something went wrong");
builder.setMessage("Enter a valid NUP and Password");
displayAlert("input_error");
}
else
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, feed_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
if (code.equals("Feedback success")) {
builder.setTitle("Feedback success...");
builder.setMessage(message);
displayAlert(code);
} else {
builder.setTitle("Feedback Failed...");
builder.setMessage("Please check your internet connection");
displayAlert(code);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Nup", nup);
params.put("topik", topik);
params.put("subject", subj);
params.put("feedback", feed);
return params;
}
};
MySingleton.getInstance(Feedback.this).addToRequestque(stringRequest);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
}
}
public void displayAlert(final String code) {
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (code.equals("Feedback success")) {
subject.setText("");
feedback.setText("");
}
else if (code.equals("input_error")) {
subject.setText("");
feedback.setText("");
}
else if (code.equals("Feedback Failed...")) {
subject.setText("");
feedback.setText("");
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
我在我的其他页面使用了相同的代码而且我没有得到错误但是当我实现相同的代码时我得到了一个错误,我不知道错误来自哪里。有人可以帮助我吗?
FATAL EXCEPTION: AsyncTask #3
Process: com.icul.bkiisee, PID: 31153
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.app.Dialog.<init>(Dialog.java:121)
at android.app.Dialog.<init>(Dialog.java:166)
at android.support.v7.app.AppCompatDialog.<init>(AppCompatDialog.java:46)
at android.support.v7.app.AlertDialog.<init>(AlertDialog.java:97)
at android.support.v7.app.AlertDialog$Builder.create(AlertDialog.java:929)
at com.icul.bkiisee.Feedback.displayAlert(Feedback.java:207)
at com.icul.bkiisee.Feedback$SendFB.doInBackground(Feedback.java:132)
at com.icul.bkiisee.Feedback$SendFB.doInBackground(Feedback.java:116)
at android.os.AsyncTask$2.call(AsyncTask.java:304)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
这是我在运行应用程序时遇到的错误
答案 0 :(得分:1)
我删除了doInBackground并将对话框放在onClick按钮内。它解决了我的问题
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feedback);
builder = new AlertDialog.Builder(Feedback.this);
listTopic = new ArrayList<String>();
listTopic.add("Interface");
listTopic.add("Menu");
listTopic.add("Features Request");
listTopic.add("Bugs");
listTopic.add("Others");
topic = (Spinner) findViewById(R.id.spinner);
listarray = new ArrayAdapter<String>(getApplicationContext(), simple_dropdown_item_1line, listTopic);
listarray.setDropDownViewResource(simple_dropdown_item_1line);
topic.setAdapter(listarray);
topic.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
topik = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
subject = (EditText) findViewById(R.id.subJect);
feedback =(EditText) findViewById(R.id.feedBack);
btnsend =(Button) findViewById(R.id.btnSend);
btnsend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
subj = subject.getText().toString();
feed = feedback.getText().toString();
nup = SaveSharedPreference.getNup(getApplicationContext());
if (subj.equals("")||feed.equals("")){
builder.setTitle("Something went wrong");
builder.setMessage("Enter a valid Subject and Feedback");
displayAlert("error");
}
else
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, feed_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String code = jsonObject.getString("code");
String message = jsonObject.getString("message");
if (code.equals("Feedback success")) {
builder.setTitle("Feedback success...");
builder.setMessage(message);
displayAlert(code);
} else {
builder.setTitle("Feedback Failed...");
builder.setMessage("Please check your internet connection");
displayAlert(code);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Nup", nup);
params.put("topik", topik);
params.put("subject", subj);
params.put("feedback", feed);
return params;
}
};
MySingleton.getInstance(Feedback.this).addToRequestque(stringRequest);
}
}
});
}
public void displayAlert(final String code) {
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (code.equals("Feedback success")) {
subject.setText("");
feedback.setText("");
}
else if (code.equals("error")) {
subject.setText("");
feedback.setText("");
}
else if (code.equals("Feedback Failed...")) {
subject.setText("");
feedback.setText("");
}
}
});
alertDialog = builder.create();
alertDialog.show();
}