具有未知来源的奇怪的空指针异常

时间:2017-09-15 07:07:29

标签: java android nullpointerexception

我在此代码中获取空指针异常,但我无法跟踪其源.Normally Exception提供有关哪个活动正在生成它的详细信息但它是不同的。它仅在某些设备上发生。我在互联网上搜索了这个例外,但却一无所获。我将不胜感激任何帮助。感谢

   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();
        }
    }
}

我在某些设备上遇到此异常。它完全适用于我测试自己的android kitkat和果酱豆。但我得到关于棒棒糖,棉花糖,牛轧糖的崩溃报告。

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)

1 个答案:

答案 0 :(得分:0)

我现在能够知道NPE的起源,我使用this在线工具回溯了这个例外。并做了。谢谢大家的指导。