使用数据库中的url显示Webview

时间:2014-01-27 12:30:27

标签: android webview

我是Android编程新手。我试图从数据库中提取URL链接并打开WebView,但我的应用程序崩溃了我的代码。请让我知道错误。

public class eye extends ListActivity {
     String text;

    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> eyeLists;

    // url to get video link
    private static String url_all_eye = "http://www.test.org.in/testapp/get_eye.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_EYELIST = "eyelist";
    private static final String TAG_VL = "videolink";    

    // products JSONArray
    JSONArray eyeList = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        eyeLists = new ArrayList<HashMap<String, String>>();

        // Loading videolink in Background Thread
        new LoadAllEyes().execute();


}

    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllEyes extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(eye.this);
            pDialog.setMessage("Loading video. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }


        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL `
            JSONObject json = jParser.makeHttpRequest(url_all_eye, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("Eyes: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {

                    // Getting Videolink
                    JSONArray eyeList = json.getJSONArray(TAG_EYELIST);

                    JSONObject eye = eyeList.getJSONObject(0);

                    setContentView(R.layout.webviews);
                    WebView webview = (WebView) findViewById(R.id.webView1);

                    webview.getSettings().setJavaScriptEnabled(true);

                    //show webview
                    findViewById(R.id.webView1).setVisibility(View.VISIBLE);
                      webview.loadUrl(eye.getString(TAG_VL));

                } else {
                    // no videolink found

                    Intent i = new Intent(getApplicationContext(),
                            eye.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }      

        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting video link


            pDialog.dismiss();

        }

    }

    }

LOG CAT ERROR

这是我的logcat错误

01-27 07:34:24.653: E/WindowManager(21541):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
01-27 07:34:24.653: E/WindowManager(21541):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
01-27 07:34:24.653: E/WindowManager(21541):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
01-27 07:34:24.653: E/WindowManager(21541):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1411)
01-27 07:34:24.653: E/WindowManager(21541):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-27 07:34:24.653: E/WindowManager(21541):     at  android.os.Looper.loop(Looper.java:137)
01-27 07:34:24.653: E/WindowManager(21541):     at android.app.ActivityThread.main(ActivityThread.java:5083)
01-27 07:34:24.653: E/WindowManager(21541):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 07:34:24.653: E/WindowManager(21541):     at java.lang.reflect.Method.invoke(Method.java:515)
01-27 07:34:24.653: E/WindowManager(21541):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-27 07:34:24.653: E/WindowManager(21541):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-27 07:34:24.653: E/WindowManager(21541):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您无法在后台线程中访问或操作Android视图,即您应该在主UI线程中执行findViewById()setContentView()等任务。

有很多方法可以实现这一目标。你可以发送一个Intent来在main threan中执行它,或者你可能希望使用runOnUiThread()来运行你的代码片段。