应用程序WebViewer(进程com.example.WebViewer)意外停止。请再试一次

时间:2014-04-05 20:42:03

标签: java android webview

我在Eclipse Android Emulator ::

上运行应用程序时收到此警报错误消息
The application WebViewer (process com.example.WebViewer) 
has stopped unexpectedly. Please try again

以下是我所拥有的MainActivity.java文件中的所有代码。如果您需要更多密码,请通知我。

MainActivity.java

package com.example.webviewer;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    } 


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                WebView ourBrow = (WebView) rootView.findViewById(R.id.Browser);
                try{
                    ourBrow.loadUrl("http://www.youtube.com/");
                } catch (Exception e){
                    e.printStackTrace();
                }
                return ourBrow; 
            }
        }

}

fragment_main.xml

<RelativeLayout 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="com.example.webviewer.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <WebView
        android:id="@+id/Browser"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

这里有什么建议。

1 个答案:

答案 0 :(得分:1)

您必须在WebView上的onCreateView()内声明PlaceHolderFragment,因为它是您的片段布局的一部分。从onCreate()中删除它。像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
WebView ourBrow = (WebView) rootView.findViewById(R.id.Browser);
        try{
            ourBrow.loadUrl("http://www.youtube.com/");
        } catch (Exception e){
            e.printStackTrace();
        } 
}