我的片段很少(他们只用webview打开网站)。我想在ActionBar的右侧添加一个刷新按钮,它应该只刷新当前片段的webview。我创建了menu_main.xml,drawable图标等,但我无法在片段上使用它。这里我的代码有失败。
package net.xxxxx.ccccc;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import static net.xxxxx.ccccc.R.id.vwVatan;
public class VatanFragment extends Fragment {
Activity titleChange;
public VatanFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View vtn = inflater.inflate(R.layout.fragment_vatan, container, false);
WebView webVatan = (WebView) vtn.findViewById(vwVatan);
WebSettings webSettings6 = webVatan.getSettings();
webSettings6.setJavaScriptEnabled(true);
webVatan.loadUrl("http://www.somesite.com/");
webVatan.setWebViewClient(new WebViewClient());
titleChange.setTitle("Vatan");
return vtn;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
titleChange = (MainActivity) activity;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
WebView wvVatan = (WebView) vtn.getChildAt();
switch (item.getItemId()){
case R.id.refresh:
vwVatan.reload();
return true;
}
}
}