簡體   English   中英

Android:build.gradle中的API兼容性問題

[英]Android: API Compatibility issues in build.gradle

當我使用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>

我會懷疑,建議使用任何教程runOnUiThread()AsyncTasksdoInBackground()或使用任何教程runOnUIThread()AsyncTasks ' onPostExecute()它已經運行在UI Thread )。

您正在獲取(或將確定是否不是這個ExceptionNetworkOnMainThread 如果要在doInBackground()完成后更新UI,則應在onPostExecute()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM