实时崩溃java.lang.NullPointerException

时间:2017-07-02 09:34:57

标签: java android nullpointerexception

我得到了我的应用程序,这对我很好,从不崩溃,但在谷歌播放它显示实时崩溃java.lang.NullPointerException,其中2个在GalleryActivity.onCreate,我几乎100崩溃所有这些只在Galaxy设备上的Android V6和一些V7!

GalleryActivity.onCreate google play Stack Traces 1

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
  at android.app.ActivityThread.access$1100(ActivityThread.java:230)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:7409)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: 
  at java.util.TimSort.sort(TimSort.java:169)
  at java.util.Arrays.sort(Arrays.java:2010)
  at com.kurdish.flag.GalleryActivity.onCreate(Unknown Source:0)
  at android.app.Activity.performCreate(Activity.java:6904)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267)`

GalleryActivity.onCreate google play Stack Traces 2

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
  at android.app.ActivityThread.-wrap14(ActivityThread.java:0)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6682)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.NullPointerException: 
  at java.util.Arrays.sort(Arrays.java:1450)
  at com.kurdish.flag.GalleryActivity.onCreate(Unknown Source:0)
  at android.app.Activity.performCreate(Activity.java:6942)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)    

GalleryActivity Java文件

package com.kurdish.flag;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;

public class GalleryActivity extends AppCompatActivity {

    File file;
    GridView gallery_grid;
    GalleryImageAdapter adapter;
    Toolbar toolbar;
    ImageView BackgroundBlurLayer;
    private String[] FilePathStrings;
    private String[] FileNameStrings;
    private File[] listFile;
    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_gallery);
        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(getString(R.string.gallery));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        initilizeVariable();

        adView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().addTestDevice(getResources().getString(R.string.test_device_id)).build();
        adView.loadAd(adRequest);

        BackgroundBlurLayer = (ImageView) findViewById(R.id.BackgroundBlurLayer);
        BackgroundBlurLayer.setImageResource(AllStaticData.BackgroundImage);

        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Toast.makeText(GalleryActivity.this, "Error! No SDCARD Found!", Toast.LENGTH_LONG).show();
        } else {
            file = new File(Environment.getExternalStorageDirectory(), "InstasquareEditor/");
            file.mkdirs();
        }

        if (file.isDirectory()) {
            listFile = file.listFiles();
            FilePathStrings = new String[listFile.length];
            FileNameStrings = new String[listFile.length];

            for (int i = 0; i < listFile.length; i++) {
                FilePathStrings[i] = listFile[i].getAbsolutePath();
                FileNameStrings[i] = listFile[i].getName();
                Log.i("path", "" + FilePathStrings[i]);
                Log.i("path", "" + FileNameStrings[i]);
            }
        }

        Arrays.sort(FilePathStrings, Collections.reverseOrder());

        adapter = new GalleryImageAdapter(getApplicationContext(), FilePathStrings, FileNameStrings);

        gallery_grid.setAdapter(adapter);

        gallery_grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                TextView txt_path = (TextView) view.findViewById(R.id.txt_path);
                Intent i = new Intent(getApplicationContext(), ShareActivity.class);
                i.putExtra("FilePathStrings", FilePathStrings);
                i.putExtra("FileNameStrings", FileNameStrings);
                i.putExtra("current", position);
                startActivity(i);
            }
        });
    }

    private void initilizeVariable() {
        gallery_grid = (GridView) findViewById(R.id.gallery_grid_view);
        BackgroundBlurLayer = (ImageView) findViewById(R.id.BackgroundBlurLayer);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
        gallery_grid.setAdapter(adapter);

    }
}

非常感谢

1 个答案:

答案 0 :(得分:2)

NullPointerException错误用于代码中的数组,而数组则由SD卡中的数据填充。您是否在Android 6或更高版本中处理用户访问SD卡的权限? 像这样使用ActivityCompat获取权限:

ActivityCompat.requestPermissions(ActivityCatList.this,new String [] {android.Manifest.permission.READ_EXTERNAL_STORAGE},0);