2013-02-20 60 views
2

我有一個很奇怪的問題。在使用SherlockActionBar時,我成功設置了上下文操作欄。當我選擇其中一個動作模式按鈕時,顯示吐司消息正常工作。當我通過我的方法電話saveRing()交換時,我得到一個關閉的力量。錯誤必須在我的saveRing()方法中,但我無法弄清楚。當我從彈出的上下文菜單調用它時,saveRing方法正常工作。Error OnActionItemClicked

我saveRing():

public boolean saveRing(int raw_resource, String title) { 
     byte[] buffer = null; 
     InputStream fIn = getBaseContext().getResources() 
       .openRawResource(raw_resource); 
     int size = 0; 

     try { 
      size = fIn.available(); 
      buffer = new byte[size]; 
      fIn.read(buffer); 
      fIn.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } 

     String path = "/sdcard/media/audio/ringtones/"; 
     String filename = title + ".mp3"; 

     boolean exists = (new File(path)).exists(); 
     if (!exists) { 
      new File(path).mkdirs(); 
     } 
     FileOutputStream save; 
     try { 
      save = new FileOutputStream(path + filename); 
      save.write(buffer); 
      save.flush(); 
      save.close(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return false; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return false; 
     } 
     sendBroadcast(
       new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri 
         .parse("file://" + path + filename))); 

     File k = new File(path, filename); 

     ContentValues values = new ContentValues(); 
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
     values.put(MediaStore.MediaColumns.TITLE, title); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.Audio.Media.ARTIST, "Epic Meal Time "); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); 
     values.put(MediaStore.Audio.Media.IS_ALARM, false); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

     Uri pURI = MediaStore.Audio.Media.getContentUriForPath(k 
       .getAbsolutePath()); 

     // remove entry every time so we don't get duplicate entries and have a 
     // problem setting a 2nd time 
     getContentResolver().delete(
       pURI, 
       MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() 
         + "\"", null); 

     Uri nURI = getContentResolver().insert(pURI, 
       values); 

     RingtoneManager.setActualDefaultRingtoneUri(this, 
       RingtoneManager.TYPE_RINGTONE, nURI); 
     Toast.makeText(this, title + " Ringtone set", 
       Toast.LENGTH_LONG).show(); 

     return true; 
    } 

的logcat:

02-19 21:38:47.691: E/AndroidRuntime(20492): FATAL EXCEPTION: main 
02-19 21:38:47.691: E/AndroidRuntime(20492): java.lang.NullPointerException 
02-19 21:38:47.691: E/AndroidRuntime(20492): at vartanian.android.epicmealtimepro.Tab3Fragment$mActionModeCallback.onActionItemClicked(Tab3Fragment.java:346) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.actionbarsherlock.internal.ActionBarSherlockNative$ActionModeCallbackWrapper.onActionItemClicked(ActionBarSherlockNative.java:243) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:2552) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.app.ActionBarImpl$ActionModeImpl.onMenuItemSelected(ActionBarImpl.java:931) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:514) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:99) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at android.view.View.performClick(View.java:4091) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at android.view.View$PerformClick.run(View.java:17036) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at android.os.Handler.handleCallback(Handler.java:615) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at android.os.Handler.dispatchMessage(Handler.java:92) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at android.os.Looper.loop(Looper.java:137) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at android.app.ActivityThread.main(ActivityThread.java:4962) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at java.lang.reflect.Method.invoke(Method.java:511) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
02-19 21:38:47.691: E/AndroidRuntime(20492): at dalvik.system.NativeStart.main(Native Method) 
02-19 21:38:49.262: I/Process(20492): Sending signal. PID: 20492 SIG: 9 

謝謝! 編輯:這是我在346行的代碼:ma.saveRing(R.raw.quaq_language, values[2]);

有錯誤的行取決於ListFragment中哪個項目調用CAB。我把它設置有switch聲明是這樣的:

case R.id.ringtone: 
       switch (viewId) { 
       case 0: 
        ma.saveRing(R.raw.ohhh_gurllll, values[0]); 
        break; 
       case 1: 
        ma.saveRing(R.raw.pork_balls, values[1]); 
        break; 

viewId是ListFragment中的位置。 問題似乎是值[]爲空,但我不知道爲什麼。這是它初始化的地方:

public class Tab3Fragment extends SherlockListFragment { 

    String[] values = new String[] { "Ohh Gurl", "Pork Balls", "Language", 
      "Really Good Idea", "Rice Paper Bacon Condom", 
      "Rise and Shine", "Roll One Up Homie", 
      "Save The Hate For Twitter", "Snap, Crackle, Pop", 
      "Let's Get Girls", "Spanish", 
      "We Make Steak Look Like Cabbage", "Wake Up McDonalds", 
      "Super Moist", "Stupid, Tasty Birds", "Bacon Moment", 
      "Today We Eat Smart", "We Are Gonna Die On Youtube", 
      "Drunk Off Pancakes", "Ketchup", "We Eat All Our Babies", 
      "Maximum Meat Experience", "Stop Hating", "What Up Bitches", 
      "Whatcha Know About Bullets", 
      "Whatcha Know About Terrible Food", "CANADA", "Beiber Concert", 
      "Ending Riceism", "You Ain't Got What We Got", "F*cking Noob", 
      "Constructed Meat Base", "Want Some Of These Nuts" }; 

這是填充arrayadapter的字符串數組。

+0

能否請您發佈此代碼:在vartanian.android.epicmealtimepro.Tab3Fragment $ mActionModeCallback.onActionItemClicked(Tab3Fragment。 java:346) – 2013-02-20 19:55:30

+0

@GauravArora編輯過的文章 – 2013-02-20 20:03:18

+3

在switch語句上放置一個斷點並逐步完成。從你顯示的內容(假設你的R.raw資源是正確設置的),它看起來像是[x]或者'ma'都是空的。 – Simon 2013-02-20 20:07:47

回答

0

所以我終於明白了。我將getSherlockActivity()傳入我的saveRing(),並在整個方法中使用該上下文。我不知道爲什麼會有用的SherlockFragmentActivity方面的問題,但是,這是它的外觀:

public boolean saveRing(int raw_resource, String title, Context context) { 
     byte[] buffer = null; 
     InputStream fIn = context.getResources() 
       .openRawResource(raw_resource); 
     int size = 0; 

     try { 
      size = fIn.available(); 
      buffer = new byte[size]; 
      fIn.read(buffer); 
      fIn.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      return false; 
     } 

     String path = "/sdcard/media/audio/ringtones/"; 
     String filename = title + ".mp3"; 

     boolean exists = (new File(path)).exists(); 
     if (!exists) { 
      new File(path).mkdirs(); 
     } 
     FileOutputStream save; 
     try { 
      save = new FileOutputStream(path + filename); 
      save.write(buffer); 
      save.flush(); 
      save.close(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return false; 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return false; 
     } 
     context.sendBroadcast(
       new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri 
         .parse("file://" + path + filename))); 

     File k = new File(path, filename); 

     ContentValues values = new ContentValues(); 
     values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath()); 
     values.put(MediaStore.MediaColumns.TITLE, title); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.Audio.Media.ARTIST, "Epic Meal Time "); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false); 
     values.put(MediaStore.Audio.Media.IS_ALARM, false); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, false); 

     Uri pURI = MediaStore.Audio.Media.getContentUriForPath(k 
       .getAbsolutePath()); 

     // remove entry every time so we don't get duplicate entries and have a 
     // problem setting a 2nd time 
     context.getContentResolver().delete(
       pURI, 
       MediaStore.MediaColumns.DATA + "=\"" + k.getAbsolutePath() 
         + "\"", null); 

     Uri nURI = context.getContentResolver().insert(pURI, 
       values); 

     RingtoneManager.setActualDefaultRingtoneUri(context, 
       RingtoneManager.TYPE_RINGTONE, nURI); 
     Toast.makeText(context, title + " Ringtone set", 
       Toast.LENGTH_LONG).show(); 

     return true; 
    }