当我使用API 19而不是API 8时,我的应用程序在没有任何日志的情况下停止工作
我正在使用本教程中的代码:http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
这是完美运行的gradle配置:
apply plugin: 'android'
android {
compileSdkVersion 8
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 8
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
我想更新它以便它可以使用我正在使用的另一个代码并需要最新的API,因此我将gradle配置修改为:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
在没有任何日志消息的情况下打开特定屏幕时突然停止工作,这是该屏幕的.java在打开时失败(它从.php脚本上的json调用接收数据并在webView上显示):
package com.example.androidhive;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class JobDescriptionActivity extends Activity {
TextView txtJobID;
TextView txtCompanyName;
TextView txtCompanyHeadline;
TextView txtJobLocation;
WebView webViewDesc;
String job_id;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String url_product_detials = "http://10.0.2.2/android_connect/tuivel/get_job_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_JOB = "job";
private static final String TAG_PID = "job_id";
private static final String TAG_JOBLOCATION = "job_location";
private static final String TAG_JOBID = "job_id";
private static final String TAG_COMPANYNAME = "company_name";
private static final String TAG_COMPANYHEADLINE = "company_headline";
private static final String TAG_DESCRIPTION = "job_description";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.job_description);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
job_id = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// send result code 100 to notify about product update
setResult(100);
//finish();
}
/**
* Background Async Task to Get complete product details
* */
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(JobDescriptionActivity.this);
pDialog.setMessage("Loading job details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("job_id", job_id));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_product_detials, "GET", params);
// check your log for json response
Log.d("Single Job Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json.getJSONArray(TAG_JOB); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
//txtJobTitle = (TextView) findViewById(R.id.jobDescTitle);
txtJobID = (TextView) findViewById(R.id.jobID);
txtCompanyName = (TextView) findViewById(R.id.companyName);
txtCompanyHeadline = (TextView) findViewById(R.id.companyHeadline);
txtJobLocation = (TextView) findViewById(R.id.jobLocation);
webViewDesc = (WebView) findViewById(R.id.webView);
// display product data in EditText
txtJobID.setText(product.getString(TAG_JOBID));
txtCompanyName.setText(product.getString(TAG_COMPANYNAME));
txtCompanyHeadline.setText(product.getString(TAG_COMPANYHEADLINE));
txtJobLocation.setText(product.getString(TAG_JOBLOCATION));
webViewDesc.loadData("<html>"+product.getString(TAG_DESCRIPTION)+"</html>", "text/html",null);
}else{
// product with pid not found
}
} 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 got all details
pDialog.dismiss();
}
}
}
如果需要XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F7F7F7"
android:orientation="vertical" >
<!-- Job id (pid) - will be HIDDEN - here if needed -->
<TextView
android:id="@+id/jobID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F1F1F1"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/rihanna"/>
</LinearLayout>
<!-- Company Name-->
<TextView
android:id="@+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:layout_marginLeft="5dip"
android:text="Company"
android:textColor="#555555"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Company Headline -->
<TextView
android:id="@+id/companyHeadline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/companyName"
android:textColor="#666666"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_marginLeft="5dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Job Title" />
<!-- Job Location -->
<TextView
android:id="@+id/jobLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/companyHeadline"
android:layout_marginLeft="5dip"
android:textColor="#777777"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Job Info" />
</RelativeLayout>
<WebView
android:id="@+id/webView"
android:layout_height="0dp"
android:layout_width="match_parent"
android:background="#FFFFFF"
android:layout_weight="0.9" />
</LinearLayout>
答案 0 :(得分:0)
我怀疑是否有任何建议使用runOnUiThread()
AsyncTasks
中的doInBackground()
或runOnUIThread()
中使用AsyncTasks
的任何教程&#39; onPostExecute()
(它已在UI Thread
上运行)。
你得到的Exception
(或者肯定不会是这个)NetworkOnMainThread
是doInBackground()
。如果您想在onPostExecute()
完成后更新用户界面,则应在{{1}}中执行此操作。