Android代码错误
可能重复: 此处给出了发生错误的程序代码-Android代码错误,android,Android,可能重复: 此处给出了发生错误的程序代码- package com.example.annapurna; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NavUtils; import android.view.Menu; import android.view.MenuItem; import andro
package com.example.annapurna;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DisplayContri extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText editText1;
//EditText text2;
//EditText text3;
// url to create new product
private static String url_create_product = "http://10.0.2.2/annapurna_connect/userold.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_contri);
//Edit text
editText1 = (EditText) findViewById(R.id.editText1);
//text2 = (EditText) findViewById(R.id.editText2);
//text3 = (EditText) findViewById(R.id.editText3);
//Create Button
Button button1 = (Button) findViewById(R.id.button1);
//button click event
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Creating new Contribution entry in background thread
new CreateContribution().execute("");
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateContribution extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DisplayContri.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args)
{
String name = editText1.getText().toString();
//String age = (text2.getText().toString());
//String id = (text3.getText().toString());
Toast toast2=Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_LONG);//This is where the error shows
toast2.show();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
//params.add(new BasicNameValuePair("age", age));
//params.add(new BasicNameValuePair("id", id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), Annapurna.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_display_contri, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void contributor(View view) {
Intent intent = new Intent(this, Contributor.class);
startActivity(intent);
}
}
有一个用于连接的Jsonparsor.java类。
在上面的代码中,doInBackground方法没有执行,我通过添加toast测试了应用程序,发现它没有进入doInBackground方法并一直执行到preexecute的最后一个末尾。请检查是否在主线程以外的线程上创建AsyncTask 您正试图从异步任务的后台更新UI 这是办不到的
如果您有任何Toast或任何其他更新,请确保将其从doInBackground删除到UI 任何背景,比如什么时候,什么地方发生的?代码示例?如果您正在处理任何邮件应用程序,以便检查您从日志发送的附件路径,听起来您正试图显示对话框等,或者从后台线程更改布局,如doInBackground。非常感谢您…我在过去三周中一直使用相同的代码。但现在终于完成了。没有问题,我的朋友。。我很高兴它帮助了你。现在,单击此答案左侧的勾号接受此答案如何:
package com.example.annapurna;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class DisplayContri extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText editText1;
//EditText text2;
//EditText text3;
// url to create new product
private static String url_create_product = "http://10.0.2.2/annapurna_connect/userold.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_contri);
//Edit text
editText1 = (EditText) findViewById(R.id.editText1);
//text2 = (EditText) findViewById(R.id.editText2);
//text3 = (EditText) findViewById(R.id.editText3);
//Create Button
Button button1 = (Button) findViewById(R.id.button1);
//button click event
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Creating new Contribution entry in background thread
new CreateContribution().execute("");
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateContribution extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DisplayContri.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args)
{
String name = editText1.getText().toString();
//String age = (text2.getText().toString());
//String id = (text3.getText().toString());
Toast toast2=Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_LONG);//This is where the error shows
toast2.show();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
//params.add(new BasicNameValuePair("age", age));
//params.add(new BasicNameValuePair("id", id));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), Annapurna.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_display_contri, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
public void contributor(View view) {
Intent intent = new Intent(this, Contributor.class);
startActivity(intent);
}
}