当我在底部栏中选择片段时,我在屏幕上显示片段时遇到了一些问题。
这是一些上下文:
MainActivity.java
package com.pkg.android;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.helpjobapp.android.ExploreFragment;
import com.helpjobapp.android.HomeFragment;
import com.helpjobapp.android.MyAccountFragment;
import com.helpjobapp.android.MyServicesFragment;
import com.helpjobapp.android.R;
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = findViewById(R.id.bottom_nav_view);
bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener);
openFragment(HomeFragment.newInstance("", ""));
}
public void openFragment(Fragment fragment) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.home_fragment:
openFragment(HomeFragment.newInstance("", ""));
return true;
case R.id.explore_fragment:
openFragment(ExploreFragment.newInstance("", ""));
return true;
case R.id.myservices_fragment:
openFragment(MyServicesFragment.newInstance("", ""));
return true;
case R.id.myaccount_fragment:
openFragment(MyAccountFragment.newInstance("", ""));
return true;
}
return false;
}
};
}
HomeFragment.java
package com.pkg.android;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}
此代码使应用程序停止。它已经打开并显示停止的消息。
我错过了什么吗?
如果我在 OnCreate 方法中注释此方法,它会停止崩溃。
// openFragment(HomeFragment.newInstance("", ""));
编辑:Logcat 输出
8003-8003/com.helpjobapp.android E/FragmentManager: No view found for id 0x7f090030 (com.helpjobapp.android:id/container) for fragment HomeFragment{cfd48e4 #0 id=0x7f090030}
2020-03-25 00:50:3
2020-03-25 00:25:09.325 5523-5523/com.helpjobapp.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.helpjobapp.android, PID: 5523
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.helpjobapp.android/com.helpjobapp.android.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.helpjobapp.android.MainActivity" on path: DexPathList[[zip file "/data/app/com.helpjobapp.android-oVVLDVUTFxke6gTer7JWSg==/base.apk"],nativeLibraryDirectories=[/data/app/com.helpjobapp.android-oVVLDVUTFxke6gTer7JWSg==/lib/x86, /system/lib, /vendor/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2718)
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_home"
android:checkable="true"
android:fontFamily="@font/poppins_regular"
android:icon="@drawable/ic_home"
android:title="@string/nav_home"
android:visible="true" />
<item
android:id="@+id/nav_search"
android:checkable="true"
android:icon="@drawable/ic_search"
android:title="@string/nav_search"
android:visible="true" />
<item
android:id="@+id/nav_myservices"
android:checkable="true"
android:icon="@drawable/ic_myservices"
android:title="@string/nav_myservices"
android:visible="true" />
<item
android:id="@+id/nav_account"
android:checkable="true"
android:icon="@drawable/ic_account"
android:title="@string/nav_account"
android:visible="true" />
</group>
</menu>