从First Fragment

时间:2017-07-28 17:59:04

标签: eclipse android-layout android-studio android-fragments android-webview

我有两个碎片。 First Fragment命名为Fragment1,Second Fragment命名为physics。第一片段有两个按钮。我想在第二个片段中显示一个本地html页面(按下Button1时按A.html,按下Button2时按B.html),即physics.java

My Fragment.java包含以下代码



package com.nepalpolice.mnemonics;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

/**
 * Created by Sagar on 2017/09/23.
 */

public class Fragment1 extends Fragment implements View.OnClickListener{

        public Button button;


    public Fragment1() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
        button = (Button) view.findViewById(R.id.button1);
        button = (Button) view.findViewById(R.id.button2);


        return view;
    }

    @Override
    public void onClick(View v) {


        Intent intent = new Intent(getActivity().getApplicationContext(), physics.class);

        switch (v.getId()) {
            case R.id.button1:
                intent.putExtra("url", "file:///android_asset/B.html");
                startActivity(intent);
                break;

            case R.id.button2:
                intent.putExtra("url", "file:///android_asset/A.html");
                startActivity(intent);
                break;

            default:
                break;


        }
    }
}




和My SecondFragment包含以下代码



package com.nepalpolice.mnemonics;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by Sagar on 2017/09/23.
 */

public class physics extends Fragment {

  WebView myWebView;
    public physics() {
        // 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_fragment2, container,  false);


        String url = getActivity().getIntent().getStringExtra("url");
        myWebView=(WebView)rootView.findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl(url);

        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
        return rootView;

    }
}




和main fragment_fragment1.xml包含



<FrameLayout 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"
    tools:context="com.nepalpolice.mnemonics.Fragment1">


    <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" >



        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentTop="true"
            android:text="button01"
            android:onClick="onClick"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_alignParentTop="true"
            android:text="button02"
            android:onClick="onClick"/>




    </LinearLayout>

</FrameLayout>
&#13;
&#13;
&#13;

和Secondfragment.xml有

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<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" >

    <WebView
        android:id="@+id/webview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
&#13;
&#13;
&#13;

但是我遇到了以下错误。

java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button1'

我几乎完成了我的项目。如果可以的话......这对我来说意味着很多。

0 个答案:

没有答案