-2

I am new to programming for android, I am developing an application in android studio in which I have to press buttons to enter numbers, my problem is to test the application on a phone in debug mode, when I opened the app is closed immediately by sending this message: "MyApp Unfortunately has stopped". Below i have my code with the Logcat. I'm using an a Samsung SM-G360T1 (ANDROID, 5.1.1, API 22)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dom.myapp.MainActivity"
    android:background="@color/colorWhite">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/relLayout">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutTopBar"
            android:layout_alignParentTop="true">

            <android.support.design.widget.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbarTop"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/white_gray_border_bottom">


                </android.support.v7.widget.Toolbar>

            </android.support.design.widget.AppBarLayout>

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutBotBar"
            android:layout_alignParentBottom="true">

            <android.support.design.widget.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/bottomNavView_Bar"
                android:background="@drawable/white_gray_border_top"
                app:menu="@menu/bottom_navigation_menu">



            </android.support.design.widget.BottomNavigationView>


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:id="@+id/relLayoutMiddle"
            android:layout_below="@+id/relLayoutTopBar"
            android:layout_above="@+id/relLayoutBotBar" />

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/container">

        </android.support.v4.view.ViewPager>

        </RelativeLayout>

</android.support.design.widget.CoordinatorLayout>

Here's my MainActivity

    package com.example.dom.myapp;
import android.os.StrictMode;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    private SectionPagerAdapter mSectionPagerAdapter;

    private ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        tabLayout.getTabAt(0).setIcon(R.drawable.ic_prayer);
        tabLayout.getTabAt(1).setIcon(R.drawable.ic_cam);
        tabLayout.getTabAt(2).setIcon(R.drawable.ic_person);

    }

    private void setupViewPager(ViewPager ViewPager) {
        SectionPagerAdapter adapter = new SectionPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1Fragment());
        adapter.addFragment(new Tab2Fragment());
        adapter.addFragment(new Tab3Fragment());
        ViewPager.setAdapter(adapter);

And here's my LogCat

    04-25 16:04:59.356 3751-3751/? E/Zygote: MountEmulatedStorage()
04-25 16:04:59.356 3751-3751/? E/Zygote: v2
04-25 16:04:59.376 3751-3751/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
04-25 16:05:00.786 3751-3751/com.example.dom.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.dom.myapp, PID: 3751
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dom.myapp/com.example.dom.myapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3120)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3219)
                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:198)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6838)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
                                                                         at com.example.dom.myapp.MainActivity.onCreate(MainActivity.java:29)
                                                                         at android.app.Activity.performCreate(Activity.java:6535)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3073)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3219) 
                                                                         at android.app.ActivityThread.access$1000(ActivityThread.java:198) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:145) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:6838) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
Dnique
  • 11
  • 1
  • 6
  • 2
    The line `mViewPager = (ViewPager) findViewById(R.id.container);` causes the problem in the first place. Apparently you don't have a `ViewPager` with the id `container` in your XML layout file so it fails to find it. – Markus Kauppinen Apr 25 '17 at 17:38
  • Check: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Veneet Reddy Apr 25 '17 at 17:40
  • This is what i put, idk if its right. i put it in the activity_main.xml ` ` – Dnique Apr 25 '17 at 19:40
  • `findViewById(R.id.tabs);` what do you expect that to do? – njzk2 Apr 25 '17 at 20:12

1 Answers1

0
<android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/container">

        </android.support.v4.view.ViewPager>

Just add the id in ViewPager in your activity_main.xml.

sanidhya pal
  • 345
  • 1
  • 9
  • i did this and still didn't work – Dnique Apr 25 '17 at 19:41
  • if u have added the id then error must have change post that error. – sanidhya pal Apr 25 '17 at 19:50
  • ok, i just re-edit the code with the new error – Dnique Apr 25 '17 at 20:12
  • In your MainActivity.java you are writing `TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);` But you dont have any TabLayout in your activity_main.xml If you want to use TabLayout then add this in AppBarLayout and below toolbar `` – sanidhya pal Apr 25 '17 at 20:36
  • I just realize by reading your comment i made a mistake. for the bottom toolbar it wasn't labeled "TabLayout" just toolbar function. Thanks again – Dnique Apr 25 '17 at 21:16