2014-02-25 61 views
1

我提出的Android應用程序,我希望當我點擊列表視圖項尋點擊項目的ID,獲取資源和設置該文件作爲ringtone.However我得到這個錯誤:在類型RingtoneManager方法setActualDefaultRingtoneUri(Context, int, Uri)是不適用的參數(new AdapterView.OnItemClickListener方法setActualDefaultRingtoneUri(上下文,INT,URI)的類型RingtoneManager不適用於參數OnItemClickListener

我怎樣才能解決這個問題?

listv.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, 
      int position, long id) { 

     String selectedName = ((ListView) view.findViewById(R.id.listView1)).getContext() 
       .toString(); 

     AssetManager assetManager = getAssets(); 

     File file = new File(Environment.getExternalStorageDirectory(), 
       "/myRingtonFolder/Audio/"); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     String path = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/MyRingtones/Audio/"; 

     File out = new File(path + "/", selectedName); 
     if (!out.exists()) { 
      try { 
       copyFile(assetManager, "Ringtone.mp3", out); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     ContentValues values = new ContentValues(); 
     values.put(MediaStore.MediaColumns.DATA, out.getAbsolutePath()); 
     values.put(MediaStore.MediaColumns.TITLE, "airh"); 
     values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3"); 
     values.put(MediaStore.MediaColumns.SIZE, out.length()); 
     values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name); 
     values.put(MediaStore.Audio.Media.IS_RINGTONE, true); 
     values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true); 
     values.put(MediaStore.Audio.Media.IS_ALARM, true); 
     values.put(MediaStore.Audio.Media.IS_MUSIC, true); 

     Uri uri = MediaStore.Audio.Media.getContentUriForPath(out.getAbsolutePath()); 
     ContentResolver mCr = getContentResolver(); 
     Uri newUri = mCr.insert(uri, values); 

     try { 
      RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, 
        newUri); 
      Settings.System.putString(mCr, Settings.System.RINGTONE, 
        newUri.toString()); 
     } catch (Throwable t) { 
      //TODO Handle exception 
     } 
     switch (position) { 
      case 0: 
       if (mp != null) { 
        mp.release(); 
        mp = null; 
       } 

       mp = MediaPlayer.create(Context.this, 
         R.raw.aint); 
       mp.start(); 

       break; 
      case 1: 
       if (mp != null) { 
        mp.release(); 
        mp = null; 
       } 

       mp = MediaPlayer.create(Context.this, 
         R.raw.airh); 
       mp.start(); 
       break; 
     } 
    } 
}); 

回答

2

您是第OnItemClickListener界面內使this將referrence它。

爲了避免這個只需將您的活動的名稱在此之前,或要求getApplicationContext()上下文

像:

RingtoneManager.setActualDefaultRingtoneUri(
    MainActivity.this, RingtoneManager.TYPE_RINGTONE, newUri); 

RingtoneManager.setActualDefaultRingtoneUri(
    getApplicationContext(), RingtoneManager.TYPE_RINGTONE, newUri); 

MediaPlayer.create(MainActivity.this, R.raw.aint);

做同樣的
+0

謝謝。有效! – user3231871

相關問題