我正在申请。 在这个应用程序中,我有一个包含listView的片段。当我点击任何项目时,它应该启动一个显示项目网站的新片段。 我的listView片段代码:
public class MyFragment extends Fragment {
private ListView mListViewASSociation;
private ListViewAssociationAdapter mListViewAssociationAdapter;
private ArrayList<ListViewAssociations> mlistviewlist;
private ProgressDialog progressDialog;
public MyFragment () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_mwl_associations, container, false);
mListViewASSociation = (ListView) rootView.findViewById(R.id.association_list);
mlistviewlist = new ArrayList<ListViewAssociations>();
String[] mAssciation_Name ;
final String Webs[];
TypedArray Icons ;
mAssciation_Name = getResources().getStringArray(R.array.Association_names);
Webs = getResources().getStringArray(R.array.Association_websites) ;
Icons = getResources().obtainTypedArray(R.array.Association_icons);
ListViewAssociations MyTitle ;
for(int i = 0 ; i<mAssciation_Name.length ; i++) {
MyTitle = new ListViewAssociations(mAssciation_Name[i],Icons.getResourceId(i,R.drawable.ic_launcher));
mlistviewlist.add(MyTitle);
}
mListViewAssociationAdapter = new ListViewAssociationAdapter(getActivity(),mlistviewlist);
mListViewASSociation.setAdapter(mListViewAssociationAdapter);
这里我正在处理点击项目
mListViewASSociation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
BlankFragment myFragment = new BlankFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.layout.fragment_mwl_associations, myFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
return rootView ;
}
我的空白片段叫做:
public class BlankFragment extends Fragment {
private ProgressDialog progressDialog;
WebView myWebView;
private String URL ;
public BlankFragment() {
// Required empty public constructor
}
public void setURL(String URL) {
this.URL = URL;
}
public String getURL() {
return URL;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.frag_blank);
myWebView.setWebViewClient(new WebViewClient());
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.requestFocusFromTouch();
myWebView.setVerticalScrollBarEnabled(true);
myWebView.setHorizontalScrollBarEnabled(true);
myWebView.setVerticalScrollBarEnabled(true);
myWebView.setHorizontalScrollBarEnabled(true);
myWebView.requestFocusFromTouch();
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setUseWideViewPort(true);
myWebView.getSettings().setLoadWithOverviewMode(true);
myWebView.addJavascriptInterface(new WebAppInterface(getActivity()), "Android");
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("www.google.com");
new LoadViewTask().execute();
return rootView ;
}
当我点击某个项目时,我的应用程序崩溃了。错误在哪里?
答案 0 :(得分:1)
您在此行onItemClick()
中所犯的错误:
变化
fragmentTransaction.replace(R.layout.fragment_mwl_associations, myFragment);
到
fragmentTransaction.replace(R.id.container, myFragment);
在这里,您为替换输入了错误的布局ID,而是添加FrameLayout
容器ID R.id.container
,用于在fragment
中添加MainActivity.java
。< / p>
参考链接会更有帮助: Fragments Basics
答案 1 :(得分:0)
删除&#34; fragmentTransaction.addToBackStack(null);&#34;可能有帮助。正如Scott所提到的,logcat堆栈(错误列表)会很有帮助。