#java #android-studio #android-fragments
Вопрос:
Поэтому я пытаюсь создать публичную библиотеку. У меня возникли проблемы с добавлением фрагмента в действие addItem, в котором содержится главное меню с кнопками, позволяющими нажимать, например, «Книга». Я понимаю, что в Интернете есть много справок, но я, похоже, не могу решить эту проблему
package com.example.app2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Toast;
public class AddNewItem extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
/*Handle action bar item clicks hee.The bar will
automatically handle clicks on the Home/Up button, so long
as you specify a parent activity in AndroidManifest.xml.
*/
switch (item.getItemId()){
case R.id.book:
Toast.makeText(getApplicationContext(),"Clicked book",Toast.LENGTH_SHORT).show();
FragmentManager fm = getFragmentManager();
//new BookFragment() is the issue
fm.beginTransaction().replace(R.id.frag, new BookFragment()).commit();
break;
case R.id.journal:
Toast.makeText(getApplicationContext(),"Clicked journal",Toast.LENGTH_SHORT).show();
break;
case R.id.cd:
Toast.makeText(getApplicationContext(),"Clicked journal",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
XML-ФАЙЛ ДЛЯ ДОБАВЛЕНИЯ НОВОГО ЭЛЕМЕНТА
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".AddNewItem">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Random Title"
android:textSize="40sp"
android:id="@ id/yes"
/>
<fragment
android:name="com.example.app2.BookFragment"
android:id="@ id/frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@ id/yes" />
</RelativeLayout>
XML-ФАЙЛ ГЛАВНОГО МЕНЮ
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item
android:id="@ id/book"
android:title="Books"
android:icon="@drawable/ic_baseline_menu_book_24"
app:showAsAction="never"
/>
<item
android:id="@ id/journal"
android:title="journal"
app:showAsAction="never"
/>
<item
android:id="@ id/cd"
android:title="song"
app:showAsAction="never"
/>
</menu>
Это ошибка, которую я получаю
error: incompatible types: BookFragment cannot be converted to Fragment
fm.beginTransaction().replace(R.id.frag, new BookFragment()).commit();
Книга xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".BookFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
/>
</RelativeLayout>
код книги
package com.example.app2;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BookFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_book, container, false);
}
}
Комментарии:
1.
getSupportFragmentManager()