gpt4 book ai didi

java - android: 帮助 webview 无法从 listview fragment 打开

转载 作者:行者123 更新时间:2023-12-01 12:10:25 27 4
gpt4 key购买 nike

我只想显示 ListView 的网页 View ,但不能。我的假在哪里?如果我运行,然后单击 ListView 中的菜单到 WebView ,不幸的是已经停止了。请帮助我。

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;


}






}

故事.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;

}


}

最佳答案

无法使用 Intent 打开 fragment 。

您需要创建一个找人 fragment 的接口(interface),并在 Activity 中使用该回调将找人 fragment 替换为故事 fragment

有关更多信息,请浏览此页面,它将更好地解释您

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

关于java - android: 帮助 webview 无法从 listview fragment 打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27311520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com