2017-07-02 77 views
-1

我得到了我的應用程序,它與我工作正常,但從未崩潰,但在谷歌播放它顯示實時崩潰java.lang.NullPointerException,其中2在GalleryActivity.onCreate,我只有在Galaxy設備android v6和一些v7上,它們幾乎全部崩潰了100次!實時崩潰java.lang.NullPointerException

GalleryActivity.onCreate谷歌玩棧跟蹤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谷歌玩棧跟蹤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

我建議你在不同SDK版本的許多模擬器上測試它。請務必至少在具有最低sdk版本的設備上以及使用最新sdk的設備上測試該應用程序。 –

+0

您也可以使用FireBase在「真實」設備上進行測試。確保包含您的應用支持的最舊設備。 –

+0

感謝您的建議,我剛剛在真實設備上對FireBase進行了測試,沒有發現任何錯誤,有趣的是所有崩潰僅在Galaxy設備Android V6和一些V7 –

回答

1

NullPointerException錯誤是針對代碼中的陣列的,陣列中的數據是由SD卡中的數據填充的。您是否在Android 6或更高版本中處理了訪問用戶SD卡的權限? 使用ActivityCompat這樣獲得權限:

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

+0

感謝您的回答,我在我的MainActivity.java中獲得了以下權限: 'ActivityCompat。 requestPermissions(MainActivity.this,permissionsList.toArray(new String [permissionsList.size()]), REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);' –