Can Anyone Tell me whats the problem in my Android Studio Java Code… Trying to implement bottom Navigation Bar

huangapple go评论73阅读模式
英文:

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, &quot;Hey&quot;, Toast.LENGTH_SHORT).show();
                default:
                    Toast.makeText(MainActivity.this, &quot;Cool&quot;, 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">

&lt;item
    android:id=&quot;@+id/pending&quot;
    android:enabled=&quot;true&quot;
    android:icon=&quot;@drawable/ic_task&quot;
    android:title=&quot;App&quot;/&gt;
&lt;item
    android:id=&quot;@+id/Donetask&quot;
    android:enabled=&quot;true&quot;
    android:icon=&quot;@drawable/ic_baseline_done_24&quot;
    android:title=&quot;Done&quot;/&gt;
&lt;item
    android:id=&quot;@+id/alltasks&quot;
    android:enabled=&quot;true&quot;
    android:icon=&quot;@drawable/ic_all&quot;
    android:title=&quot;All Task&quot;/&gt;

</menu>

activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;


    &lt;fragment
        android:id=&quot;@+id/fragment&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        /&gt;


    &lt;com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;#FFF&quot;
        android:layout_alignParentBottom=&quot;true&quot;
        android:id=&quot;@+id/bottom&quot;
        app:menu=&quot;@menu/bottom_app_menu&quot;/&gt;


&lt;/RelativeLayout&gt;

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中,您正在使用:

    &lt;fragment
        android:id=&quot;@+id/fragment&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        /&gt;

一个&lt;fragment&gt;标签必须具有指向有效Fragment类的android:name。您之所以收到NullPointerException是因为您没有为&lt;fragment&gt;标签提供任何要放入其中的片段类。

如果您只想要一个空的框,而没有任何初始片段,您可以将该标签替换为&lt;FrameLayout&gt;

英文:

In your XML, you are using:

    &lt;fragment
        android:id=&quot;@+id/fragment&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        /&gt;

A &lt;fragment&gt; 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 &lt;fragment&gt; tag.

You can replace that tag with a &lt;FrameLayout&gt; if you just want an empty box without any initial fragment.

huangapple
  • 本文由 发表于 2020年9月17日 00:58:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63924774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定