0

I am Getting a null pointer exception in this code, but I am unable to track its source.Normally Exception provide details about which activity is generating it but it is different.It occurs on some devices only. I searched the exception on internet but found nothing helpful yet. I would appreciate any help. Thanks

   public class MainActivity extends AppCompatActivity {
    Button capture;
    ImageView iv;
    private Fragment fragment;
    private FragmentManager fragmentManager;
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    ViewPagerAdapter adapter;
    private AdView mAdView;
    public static BadgeView badge;
    /*private FirebaseRemoteConfig mFirebaseRemoteConfig;
    public static int remote_counter;*/
    public static int counter;
   public static final String PREFS = "Badge";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_icon_text_tabs);
        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder()//.addTestDevice("8224D21A28DA92B60EB105E7FD793AF4")
                .build();
        mAdView.loadAd(adRequest);
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
            }

            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                mAdView.setVisibility(View.VISIBLE);
            }
        });

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setOffscreenPageLimit(3);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();

    }
    private void setupTabIcons() {
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("Facebook");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.fb, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("Videos");
        tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.videos, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("Downloads");
        tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.downloading, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);

        SharedPreferences settings = getSharedPreferences(PREFS, MODE_PRIVATE);
        int temp = settings.getInt("badge_counter", 0);

        View target = tabLayout.getTabAt(2).getCustomView();
        badge = new BadgeView(this, target);
        badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
       // badge.setText(String.valueOf(temp));
        badge.setBadgeMargin(3, 0);
        //badge.show();
     //
        if (temp > 0) {
            badge.setText(String.valueOf(temp));
            badge.show();
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new Facebook_Fragment(), "Facebook");
        adapter.addFrag(new Downloads_Fragment(), "Videos");
        adapter.addFrag(new Current_Downlaod(), "Downloads");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (grantResults.length > 0 && ActivityCompat.checkSelfPermission(MainActivity.this, permissions[0]) == PackageManager.PERMISSION_GRANTED) {

            Toast.makeText(MainActivity.this, "Permission granted", Toast.LENGTH_SHORT).show();

            ((Downloads_Fragment) adapter.getItem(1)).updateDownloads();
        } else {
            Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();

        }
    }

    @Override
    public void onPause() {
        if (mAdView != null) {
            mAdView.pause();
        }
        super.onPause();
    }

    @Override
    public void onDestroy() {
        if (mAdView != null) {
            mAdView.destroy();
        }
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }
}

and I get this exception on some of the devices. It works perfectly on android kitkat and jelly bean which I have tested my self. But I get crash reports on lollipop, marshmallow, nougat.

java.lang.NullPointerException:
  at com.kpsd.activity.c.<init>(Unknown Source:0)
  at com.kpsd.a.b$1.run(Unknown Source:0)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5451)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Asad khan
  • 156
  • 7
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Fildor Sep 15 '17 at 07:07
  • are you sure is happening in this activity???? – ΦXocę 웃 Пepeúpa ツ Sep 15 '17 at 07:10
  • I have only two activities, this one and one other but the other one only play video so I think problem is here. – Asad khan Sep 15 '17 at 07:13
  • If you can include line numbers the stack trace will make more sense. – Chris Travers Sep 15 '17 at 07:17
  • @Asadkhan you are probably using proguard and these logs are from signed apk .. try using debug apk and then check the logs, I am sure you will find the exact source of NPE – shadygoneinsane Sep 15 '17 at 07:19
  • @ChrisTravers that is the main problem, I just occurs randomly, I have tested it whole day but it did not crashed on my device and two others. – Asad khan Sep 15 '17 at 07:20
  • I have no clue what your package is, but I assume the stacktrace has been proguarded. Meaning you need a filter to change it to readable code in the right classes – Zoe Sep 15 '17 at 07:31
  • @shadygoneinsane yes you are right, these logs are from signed apk, which I have received through crash report. as I mentioned, It did not crashed on 3 of my devices while debugging and that is why I am unable to understand this problem. – Asad khan Sep 15 '17 at 07:51
  • 1
    With each build, ProGuard outputs a mapping.txt file which gives you a translation between the original and obfuscated class, method, and field names. Use it to locate the problem. – b4da Sep 15 '17 at 07:56
  • Thank you all for your comments, now I am able to know about its source. the stack trace was proguarded and when I Retraced it, I was able to find its origin. Thank you again for your time. – Asad khan Sep 15 '17 at 13:05

1 Answers1

0

I am able to know the origin of the NPE now, the exception was proguarded which I retraced using this online tool. and DONE. Thank you all for guidance.

Asad khan
  • 156
  • 7
  • The linked web page is dead. You can use proguardgui for if you want a GUI interface for the conversion. https://www.guardsquare.com/en/products/proguard/manual/gui – b4da Nov 02 '20 at 10:41