我正在尝试建立一个应用程序的碎片,这是在初级水平,我没有直接坚实的知识或经验的主题。
我希望webviewfragment在应用程序启动时可见,然后再添加listview端。
从现在的情况来看,我一在手机上启动这个应用程序就会崩溃。我把错误日志贴在下面了。
误差
01-01 18:14:24.223: E/AndroidRuntime(25050): FATAL EXCEPTION: main
01-01 18:14:24.223: E/AndroidRuntime(25050): Process: com.raj.fragment, PID: 25050
01-01 18:14:24.223: E/AndroidRuntime(25050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raj.fragment/com.raj.fragment.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.ActivityThread.access$800(ActivityThread.java:138)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.os.Looper.loop(Looper.java:136)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.ActivityThread.main(ActivityThread.java:5050)
01-01 18:14:24.223: E/AndroidRuntime(25050): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 18:14:24.223: E/AndroidRuntime(25050): at java.lang.reflect.Method.invoke(Method.java:515)
01-01 18:14:24.223: E/AndroidRuntime(25050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
01-01 18:14:24.223: E/AndroidRuntime(25050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
01-01 18:14:24.223: E/AndroidRuntime(25050): at dalvik.system.NativeStart.main(Native Method)
01-01 18:14:24.223: E/AndroidRuntime(25050): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-01 18:14:24.223: E/AndroidRuntime(25050): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.Activity.setContentView(Activity.java:1929)
01-01 18:14:24.223: E/AndroidRuntime(25050): at com.raj.fragment.MainActivity.onCreate(MainActivity.java:11)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.Activity.performCreate(Activity.java:5231)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-01 18:14:24.223: E/AndroidRuntime(25050): ... 11 more
01-01 18:14:24.223: E/AndroidRuntime(25050): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.raj.fragment.WebViewFragment that is not a Fragment
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.Fragment.instantiate(Fragment.java:585)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.Fragment.instantiate(Fragment.java:561)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.app.Activity.onCreateView(Activity.java:4778)
01-01 18:14:24.223: E/AndroidRuntime(25050): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
01-01 18:14:24.223: E/AndroidRuntime(25050): ... 21 more
01-01 18:14:24.223: E/AndroidRuntime(25050): Caused by: java.lang.ClassCastException
01-01 18:14:24.223: E/AndroidRuntime(25050): ... 25 more
主要活动
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
WebViewFragment网站
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@SuppressLint("SetJavaScriptEnabled")
public class WebViewFragment extends Fragment {
private WebView mWebView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.webview, container, false);
mWebView = (WebView)view.findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com/about/");
mWebView.setWebViewClient(new WebClient());
return view;
}
public class WebClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
webview.loadUrl(url);
return true;
}
}
}
活动\主.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:name="com.raj.fragment.WebViewFragment"
android:id="@+id/webview"
android:layout_weight="1"
android:layout_height="match_parent"/>
</LinearLayout>
最佳答案
Trying to instantiate a class com.raj.fragment.WebViewFragment that is not a Fragment
日志上写的都是
改为
public class WebViewFragment extends Fragment {
因为你有
<fragment android:name="com.raj.fragment.WebViewFragment" // is a fragment
您的
Fragment
由MainActivity
托管。也可以有
class="com.raj.fragment.WebViewFragment"
或android:name="com.raj.fragment.WebViewFragment"
。不需要两者都有。同时添加@override注释
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
编辑:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// missing
}