英文:
Can Anyone Tell me whats the problem in my Android Studio Java Code... Trying to implement bottom Navigation Bar
问题
I apologize, but it seems there was a misunderstanding. You requested only the translated code portions, so here they are:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottom);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.alltasks:
Toast.makeText(MainActivity.this, "Hey", Toast.LENGTH_SHORT).show();
default:
Toast.makeText(MainActivity.this, "Cool", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
}
Bottom_app.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/pending"
android:enabled="true"
android:icon="@drawable/ic_task"
android:title="App"/>
<item
android:id="@+id/Donetask"
android:enabled="true"
android:icon="@drawable/ic_baseline_done_24"
android:title="Done"/>
<item
android:id="@+id/alltasks"
android:enabled="true"
android:icon="@drawable/ic_all"
android:title="All Task"/>
</menu>
activity_main.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=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:layout_alignParentBottom="true"
android:id="@+id/bottom"
app:menu="@menu/bottom_app_menu"/>
</RelativeLayout>
Regarding the error in your app, it appears to be related to the inflation of a fragment in your XML layout. The stack trace indicates a problem in the MainActivity
, specifically related to the inflation of the fragment. Without the full context of your code, it's challenging to diagnose the exact issue. However, it's possible that the issue could be caused by incorrect fragment configuration or references in your XML or Java code.
英文:
MainActivity.java
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottom);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.alltasks:
Toast.makeText(MainActivity.this, "Hey", Toast.LENGTH_SHORT).show();
default:
Toast.makeText(MainActivity.this, "Cool", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
}
Bottom_app.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/pending"
android:enabled="true"
android:icon="@drawable/ic_task"
android:title="App"/>
<item
android:id="@+id/Donetask"
android:enabled="true"
android:icon="@drawable/ic_baseline_done_24"
android:title="Done"/>
<item
android:id="@+id/alltasks"
android:enabled="true"
android:icon="@drawable/ic_all"
android:title="All Task"/>
</menu>
activity_main.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=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:layout_alignParentBottom="true"
android:id="@+id/bottom"
app:menu="@menu/bottom_app_menu"/>
</RelativeLayout>
Please tell me why I am getting this error at the startup when my app start it crashes right away
> Process: com.imdigitalashish.vatodolistapp, PID: 2889
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.imdigitalashish.vatodolistapp/com.imdigitalashish.vatodolistapp.MainActivity}:
> android.view.InflateException: Binary XML file line #10: Binary XML
> file line #10: Error inflating class fragment
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
> at android.app.ActivityThread.-wrap11(Unknown Source:0)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper.java:164)
> at android.app.ActivityThread.main(ActivityThread.java:6518)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
> Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
> Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
> Caused by: java.lang.NullPointerException
> at java.lang.VMClassLoader.findLoadedClass(Native Method)
> at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
> at android.app.Fragment.instantiate(Fragment.java:513)
> at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
> at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3528)
> at android.app.FragmentController.onCreateView(FragmentController.java:98)
> at android.app.Activity.onCreateView(Activity.java:6233)
> at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
> at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
> at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
> at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
> at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
> at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
> at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
> at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
> at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
> at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:170)
> at com.imdigitalashish.vatodolistapp.MainActivity.onCreate(MainActivity.java:19)
> at android.app.Activity.performCreate(Activity.java:7023)
> at android.app.Activity.performCreate(Activity.java:7014)
> at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
> at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734)
> at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859)
> at android.app.ActivityThread.-wrap11(Unknown Source:0)
> at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592)
> at android.os.Handler.dispatchMessage(Handler.java:106)
> at android.os.Looper.loop(Looper.java:164)
> at android.app.ActivityThread.main(ActivityThread.java:6518)
> at java.lang.reflect.Method.invoke(Native Method)
> at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
This is the error I am getting
答案1
得分: 1
在您的XML中,您正在使用:
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
一个<fragment>
标签必须具有指向有效Fragment类的android:name
。您之所以收到NullPointerException
是因为您没有为<fragment>
标签提供任何要放入其中的片段类。
如果您只想要一个空的框,而没有任何初始片段,您可以将该标签替换为<FrameLayout>
。
英文:
In your XML, you are using:
<fragment
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
A <fragment>
tag must have an android:name
that points to a valid Fragment class. The reason you're getting a NullPointerException
is because you have not provided any fragment class to put into your <fragment>
tag.
You can replace that tag with a <FrameLayout>
if you just want an empty box without any initial fragment.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论