使用AsyncTask时强制关闭启动

时间:2012-03-10 16:00:01

标签: android

我在尝试以下步骤时关闭了力量。

  1. 第一次运行应用
  2. 播放音乐
  3. 按主页按钮并通过应用管理器终止应用
  4. 再次运行app并发生强制关闭
  5. 这是我的代码:

        private class LoadingFile extends AsyncTask<Void, Integer, Void> 
         {
         private ProgressDialog dialog = new ProgressDialog(BridgePlayer.this);
         protected void onPreExecute()
         {
             super.onPreExecute();
             if(isCancelled()) return;
    
             dialog.setMessage("Loading...");
             dialog.show();
         }
    
         protected Void doInBackground(Void... params) 
         {
             try
             {
                 if(isCancelled()) return null;
                 preInitialize();
                 return null;
             }
             catch(Exception e) { return null; }
         }
    
         @Override
         protected void onProgressUpdate(Integer... progress) 
         {
             super.onProgressUpdate(progress);
             /*if(SONG_PATHS.size() > 0)
             {
                 dialog.setMessage(String.valueOf(progress[0]));
                 dialog.show();
             }*/
         }
    
         protected void onPostExecute(Void result) 
         {
            super.onPostExecute(result);
    
            try
            {
            if(isCancelled()) return;
            if(dialog.isShowing()) 
            {
                dialog.dismiss();
            }
    
            initialize();
    
            this.cancel(true);
            }
            catch(Exception e) {  }
         }
    }
    
        protected void onResume()
        {
        super.onResume();
    
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_UI);
    
        String date = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH))+"-"+String.valueOf(Calendar.getInstance().get(Calendar.MONTH));
    
        blackList.clear();
        blackList.addAll(blackListToArray(sharedPrefs.getString("black_list", "")));
    
        loadFile = new LoadingFile();
        loadFile.execute();
    }
    
    
    
        private void initialize()
        {       
        registerForContextMenu(songList);
        songList.setFastScrollEnabled(true);
    
        updateSongList("");
    
        if(SONG_PATHS.contains(sharedPrefs.getString("lastSongPath", "")) && sharedPrefs.getString("lastSongPath", "")!="")
        {
            currentPosition = SONG_PATHS.indexOf(sharedPrefs.getString("lastSongPath", ""));
        }
        else currentPosition=0;
    
        if(sharedPrefs.getBoolean("isPause", true))
        {
            if(SONG_PATHS.size()>0 && SONG_PATHS.contains(sharedPrefs.getString("lastSongPath", "")))
            {
                try 
                {
                        mp.reset();
                        mp.setDataSource(SONG_PATHS.get(currentPosition));
                        mp.prepare();
                        mp.start();
                        sharedPrefs.edit().putString("lastSongPath", SONG_PATHS.get(currentPosition));
                        sharedPrefs.edit().putString("lastSongName", SONG_NAMES.get(currentPosition));
                } 
                catch (IllegalArgumentException e) {} 
                catch (IllegalStateException e) {e.printStackTrace();} 
                catch (IOException e) {e.printStackTrace();}
    
                mp.pause();
                mp.seekTo(sharedPrefs.getInt("currentDuration", 0));
                seekbar.setProgress(mp.getCurrentPosition()*1000/mp.getDuration());
            }
            pauseButton.setAlpha(128);
            playButton.setAlpha(255);
        }
        else
        {
            playButton.setAlpha(128);
            pauseButton.setAlpha(255);
        }
    
        songList.setSelection(currentPosition);
    
        int color;
        if(sharedPrefs.getString("list_background_color", "#000088").contains("Random"))
        {
            String[] colorArray = getResources().getStringArray(R.array.colorvalue);
            String colorString = colorArray[(int) (Math.random()*colorArray.length-1)];
            color = Color.parseColor(colorString);
        }
        else
        {
            color = Color.parseColor(sharedPrefs.getString("list_background_color", "#000088"));
        }
        GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {0xFF000000,color});
        songList.getRootView().setBackgroundDrawable(gradientDrawable);
        //songList.setBackgroundResource(R.drawable.kartini);
        //songList.getBackground().setAlpha(128);
        //ImageView iv = (ImageView)findViewById(R.id.background);
        //iv.setAlpha(100);
    
        Rect bounds = seekbar.getProgressDrawable().getBounds();
        seekbar.setProgressDrawable(new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, new int[] {0x11FFFFFF,0xAAFFFFFF,0x11FFFFFF}));
        seekbar.getProgressDrawable().setBounds(bounds);
    }
    
    private void preInitialize()
    {
        if(!(new File(MEDIA_PATH).exists())) (new File(MEDIA_PATH)).mkdirs(); 
        if(MEDIA_PATH!=sharedPrefs.getString("music_directory",MEDIA_PATH) || !(new File(getExternalCacheDir().getPath()+"nameFile.txt").exists()))
        {
            if((new File(sharedPrefs.getString("music_directory",MEDIA_PATH)).exists()))
            {
                MEDIA_PATH=sharedPrefs.getString("music_directory",MEDIA_PATH);
                createSongFile();
            }
        }
    
        scanDir();
    }
    

    此错误仅在我使用asynctask时出现。这个错误有什么线索吗?思考了8个小时后,我感到非常沮丧。 对不起我的英文。

    这是日志:

    03-10 23:17:09.462:ERROR / AndroidRuntime(14363):致命异常:主要 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):java.lang.RuntimeException:无法启动活动ComponentInfo {bridge.entertainment / bridge.entertainment.BridgePlayer}:java.lang.NullPointerException 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.os.Handler.dispatchMessage(Handler.java:99) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.os.Looper.loop(Looper.java:123) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.ActivityThread.main(ActivityThread.java:3683) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at java.lang.reflect.Method.invokeNative(Native Method) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at java.lang.reflect.Method.invoke(Method.java:507) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at dalvik.system.NativeStart.main(Native Method) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):引起:java.lang.NullPointerException 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at bridge.entertainment.BridgePlayer.updateSongList(BridgePlayer.java:105) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):at bridge.entertainment.BridgePlayer.onCreate(BridgePlayer.java:43) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-10 23:17:09.462:ERROR / AndroidRuntime(14363):... 11更多

0 个答案:

没有答案