我是初学者。我目前正在创建一个在用户和我的服务器之间建立连接的应用。例如,用户输入一些信息并单击按钮,然后所有信息将保存在我的服务器数据库表中。然后,在输入信息并单击按钮后,应用程序就会崩溃。 logcat和java类如下:
logcat的
06-11 17:15:41.825 12946-12990/com.example.abc.androidhive W/dalvikvm﹕ threadid=10: thread exiting with uncaught exception (group=0xa62bb288)
06-11 17:15:41.825 12946-12990/com.example.abc.androidhive E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
.
.
.
.
.
.
06-11 17:20:48.157 15150-15150/com.example.chongcng2.androidhive E/WindowManager﹕ Activity com.example.chongcng2.androidhive.NewJobActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@535a6b7c that was originally added here
android.view.WindowLeaked: Activity com.example.chongcng2.androidhive.NewJobActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@535a6b7c that was originally added here
NewJobActivity
public class NewJobActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;
EditText inputResponsibility;
EditText inputCompany;
EditText inputContact;
// url to create new product
private static String url_create_product = "http:/175.159.211.84/android_connect_server/create_job.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server_add_job);
// Edit Text
inputName = (EditText) findViewById(R.id.inputName);
inputPrice = (EditText) findViewById(R.id.inputPrice);
inputDesc = (EditText) findViewById(R.id.inputDesc);
inputResponsibility = (EditText) findViewById(R.id.inputResponsibility);
inputCompany = (EditText) findViewById(R.id.inputCompany);
inputContact = (EditText) findViewById(R.id.inputContact);
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewJobActivity.this);
pDialog.setMessage("Creating Job..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String post = inputName.getText().toString();
String location = inputPrice.getText().toString();
String salary = inputDesc.getText().toString();
String responsibility = inputResponsibility.getText().toString();
String company = inputCompany.getText().toString();
String contact = inputContact.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("post", post));
params.add(new BasicNameValuePair("location", location));
params.add(new BasicNameValuePair("salary", salary));
params.add(new BasicNameValuePair("responsibility", responsibility));
params.add(new BasicNameValuePair("company", company));
params.add(new BasicNameValuePair("contact", contact));
// 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(), AllJobsActivity.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();
}
}
}
有人可以帮忙吗?提前致谢 !
logcat 2
06-12 04:33:52.964 12196-12196/com.example.chongcng2.androidhive W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa626c288)
06-12 04:33:52.964 12196-12196/com.example.chongcng2.androidhive E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
at java.net.Socket.connect(Socket.java:842)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.example.chongcng2.androidhive.JSONParser.makeHttpRequest(JSONParser.java:51)
at com.example.chongcng2.androidhive.NewJobActivity$CreateNewProduct.onPostExecute(NewJobActivity.java:121)
at com.example.chongcng2.androidhive.NewJobActivity$CreateNewProduct.onPostExecute(NewJobActivity.java:74)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
NewJobActivity.java 2
public class NewJobActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;
EditText inputResponsibility;
EditText inputCompany;
EditText inputContact;
int success;
List<NameValuePair> params = new ArrayList<NameValuePair>();
// url to create new product
private static String url_create_product = "http://192.168.0.103/android_connect_server/create_job.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server_add_job);
// Edit Text
inputName = (EditText) findViewById(R.id.inputName);
inputPrice = (EditText) findViewById(R.id.inputPrice);
inputDesc = (EditText) findViewById(R.id.inputDesc);
inputResponsibility = (EditText) findViewById(R.id.inputResponsibility);
inputCompany = (EditText) findViewById(R.id.inputCompany);
inputContact = (EditText) findViewById(R.id.inputContact);
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewJobActivity.this);
pDialog.setMessage("Creating Job..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String post = inputName.getText().toString();
String location = inputPrice.getText().toString();
String salary = inputDesc.getText().toString();
String responsibility = inputResponsibility.getText().toString();
String company = inputCompany.getText().toString();
String contact = inputContact.getText().toString();
// Building Parameters
params.add(new BasicNameValuePair("post", post));
params.add(new BasicNameValuePair("location", location));
params.add(new BasicNameValuePair("salary", salary));
params.add(new BasicNameValuePair("responsibility", responsibility));
params.add(new BasicNameValuePair("company", company));
params.add(new BasicNameValuePair("contact", contact));
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
try {
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
// check log cat fro response
Log.d("Create Response", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), AllJobsActivity.class);
startActivity(i);
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
当您启动AssyncTask时,它将在屏幕上显示progressDialog
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewJobActivity.this);
pDialog.setMessage("Creating Job..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
如果您从AssyncTask内部启动了一项新活动,那么就会解雇&#39;之前的进度对话框,您将在屏幕上看到一个新的活动,新的上下文和新的视图,但您的AssyncTask尚未最终确定,当它到达
时protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
它将尝试解除视图中不再存在的窗口。
Activity com.example.chongcng2.androidhive.NewJobActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@535a6b7c that was originally added here
android.view.WindowLeaked: Activity com.example.chongcng2.androidhive.NewJobActivity has leaked window
我可以解释它的最佳方式XD我曾经在我的代码上看到这个错误,直到我意识到错误。我希望它可以帮到你。
修改1
protected void onPostExecute(String file_url) {
pDialog.dismiss();
try {
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(), AllJobsActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
}
编辑2:
初始化
int success;
在您声明pDialog和TextView之后的任何方法,不要在&#39;尝试&#39;
中初始化它int success = json.getInt(TAG_SUCCESS);
我会问你或者将变量初始化为0,这就是