не запускается приложение после добавления fragment

Изучаю android java пробую написать сам с нуля что то вроде браузера и столкнулся с не понятной проблемой - url грузится посредством WebView который имеет свою проблему в том что при смене ориентации экрана сбрасывает свое состояние до дефолтного (перезапускается цикл жизни activity) в сети посоветовали решение - подключить его через фрагмент но когда я попытася подключить приложение при запуске начало вылетать

андроид студия 4.1.2 SDK использую для приложения android 4.1

в build.grade я добавил библиотеку фрагментов:

dependencies {
    implementation "androidx.fragment:fragment:1.2.5"

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

XML моего mainActivity

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/TextUrl"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:hint="Введите адрес"
            android:imeOptions="actionDone" />

        <Button
            android:id="@+id/Search"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="goToMyUrl"
            android:text="Поиск"/>

        <Button
            android:id="@+id/Pages"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Вкладки" />

    </LinearLayout>

    <fragment
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        tools:layout="@layout/webview_frag" />

</LinearLayout>

java mainActivity:

package com.example.omibrowser;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import androidx.fragment.app.FragmentActivity;


import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*
        web = (WebView) findViewById(R.id.WebView);

        WebSettings ws = web.getSettings();

        ws.setJavaScriptEnabled(true);
        web.loadUrl("https://google.com");
        web.setWebViewClient(new WebViewClient());*/
    }
  /*
    @Override
    public void onBackPressed() {
        if (web.canGoBack()) {
            web.goBack();
        } else {
            super.onBackPressed();
        }
    }


    public void goToMyUrl(View view) {

        View hnview = this.getCurrentFocus();
        if (hnview != null) {
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }

        EditText myUrl = (EditText) findViewById(R.id.TextUrl);
        String strUrl = (String) myUrl.getText().toString();
        web = (WebView) findViewById(R.id.WebView);

        WebSettings ws = web.getSettings();

        ws.setJavaScriptEnabled(true);
        web.loadUrl("http://"+strUrl);
        web.setWebViewClient(new WebViewClient());
    }*/
}

XML фрагмента webview_frag:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

java класс для фрагмента:

package com.example.omibrowser;

import android.content.Context;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import android.widget.TextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class WebviewFrag extends FragmentActivity {


}

подскажите пожалуйста где искать ошибку? и так ли хорош именно для фикса проблемы сбрасывания WebViev при смене ориентации оборачивание его в фрагмент?


Ответы (0 шт):