无法从片段内的gridview加载webview

时间:2017-02-19 13:31:31

标签: android android-studio android-fragments gridview webview

我是Android编程新手。我正在尝试构建一个应用程序,它将在单击图像上加载webview。 activity_main.xml由片段组成,在这些片段中,有一个带有图像的网格视图。单击图像后,将加载相应的网站。

问题是,当我点击图片时,应用程序崩溃而不是在webview中加载网站。

这是我的WebViewActivity.java文件

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class WebViewActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);         //calls the method from super class Activity
    setContentView(R.layout.webview);          //sets the UI

    WebView myWebView = (WebView)findViewById(R.id.webView1);
    CustomWebViewClient myCustomWebViewClient = new CustomWebViewClient();
    myWebView.setWebViewClient(myCustomWebViewClient);

    String url = getIntent().getStringExtra("url");
    myWebView.loadUrl(url);

}
}

class CustomWebViewClient extends WebViewClient {
    @Override
    /**
     * Take control over the loaded url so the url is open in the current Webview and not by the OS browser
     */
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }}

这是TabFragment1.java文件:

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.GridView;



    import android.widget.AdapterView.OnItemClickListener;


public class TabFragment1 extends Fragment{

@Override
public  View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.tab_fragment1, container, false);
    GridView gridView = (GridView) view.findViewById(R.id.grid_view);
    gridView.setAdapter(new ImageAdapter(getActivity()));


    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {
            String url;
            //The url changes accordingly to the button clicked
            switch (position) {
                case 0:  url = "http://stackoverflow.com";
                    break;
                case 1:  url = "http://yahoo.fr";
                    break;
                case 2:  url = "http://bing.com";
                    break;
                default: url = "http://google.com";
                    break;
            }
            Intent intent = new Intent(getActivity(),WebViewActivity.class);
            //used to pass the url to the next activity
            intent.putExtra("url", url);
            startActivity(intent);//reference to start the activity
        }
    });

    return view;

}
}

这是webview.xml文件:

<?xml version="1.0" encoding="utf-8"?>

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

我已将此添加到androidmanifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name=".WebViewActivity"
        android:label="WebViewActivity"/>

这是我的ImagesAdapter.java:

    import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.img1, R.drawable.img2,
        R.drawable.img3, R.drawable.img4,
        R.drawable.img5, R.drawable.img6,
        R.drawable.img7, R.drawable.img8,
        R.drawable.img9, R.drawable.img10,

};

// Constructor
public ImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(0, 0, 0, 0);
    imageView.setAdjustViewBounds(true);
    imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
    return imageView;
}

}

0 个答案:

没有答案