简体   繁体   English

android:帮助webview无法从listview片段中打开

[英]android: help webview can not open from listview fragment

I just want to display a web view of a list view but can not. 我只想显示列表视图的Web视图,但不能显示。 where is my false? 我的错在哪里? if i running, and click menu in listview to webview, unfortunately has stopped. 如果我正在运行,然后单击列表视图中的菜单以进行Web视图,很遗憾已停止。 please help me. 请帮我。

FindPeopleFragment.java FindPeopleFragment.java

package info.androidhive.slidingmenu;



import android.os.Bundle;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;

import java.util.ArrayList;
import java.util.HashMap;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.Toast;


public class FindPeopleFragment extends Fragment  {

    public FindPeopleFragment(){}
      protected ListView lv;
      protected ListAdapter adapter;
      public static final String MOVIE_DETAIL_KEY = "movie";
      SimpleAdapter Adapter;
      HashMap<String, String> map;
      ArrayList<HashMap<String, String>> mylist;
      String[] Pil;
      String[] Ltn;
      String[] Gbr;


      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.activity_pulau, container,false);

            final ListView lv = (ListView) rootView.findViewById(R.id.lv);

            Pil = new String[] {"Pulau Gusung", "Binatang Laut Khas"};
            Ltn = new String[] {"Baca Selengkapnya...", "Baca Selengkapnya..."};
            Gbr = new String[] {Integer.toString(R.drawable.ic_photos),
                                Integer.toString(R.drawable.ic_photos),

                                         };

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

            for (int i = 0; i < Pil.length; i++){
                map = new HashMap<String, String>();
                map.put("list", Pil[i]);
                map.put("latin", Ltn[i]);
                map.put("gbr", Gbr[i]);
                mylist.add(map);             
            }

            Adapter = new SimpleAdapter(getActivity(), mylist, R.layout.item_kepulauan,
                      new String[] {"list", "latin", "gbr"}, new int[] {R.id.tv_nama, R.id.tv_des, R.id.imV});
            lv.setAdapter(Adapter);
            lv.setOnItemClickListener(new OnItemClickListener() {




                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                    // ListView Clicked item index
                    int itemPosition = position;

                    // ListView Clicked item value
                    String itemValue = (String) lv
                            .getItemAtPosition(position);
                    if (position == 0) {
                        Intent myIntent = new Intent(getApplicationContext(),
                                Story.class);
                        myIntent.putExtra("key", 0);
                        startActivity(myIntent);
                    }else if (position == 1) {
                        Intent myIntent = new Intent(getApplicationContext(),
                                Story.class);
                        myIntent.putExtra("key", 1);
                        startActivity(myIntent);
                    }



                }

                private Context getApplicationContext() {
                    // TODO Auto-generated method stub
                    return null;
                }
                });



            return rootView;


          }






}

Story.java Story.java

package info.androidhive.slidingmenu;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Story extends Fragment {
    @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View v = inflater.inflate(R.layout.fragment_home, container, false);    
            WebView webView = (WebView) v.findViewById (R.id.webView1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebChromeClient(new WebChromeClient());
            int pos = getActivity().getIntent().getIntExtra("key", 0);
            if (pos == 0) {
                webView.loadUrl("file:///android_asset/tampilhome.html");
            } else if (pos == 1) {
                webView.loadUrl("file:///android_asset/tampilhome.html");
            } 


            webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedError(WebView view, int errorCode,
                    String description, String failingUrl) {
                    super.onReceivedError(view, errorCode, description, failingUrl);
                    }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        }); return v;

        }


}

u can't open a fragment by using intent . 您无法使用intent打开片段。

you need to create an interface of find people fragment and use that call back in activity to replace find people fragment with story fragment 您需要创建一个查找人员片段的接口,并使用活动中的回调功能将查找人员片段替换为故事片段

for more info u go through this page , it will explain u better 欲了解更多信息,请浏览此页面,它将更好地解释您

http://developer.android.com/training/basics/fragments/communicating.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM