2013-04-14 39 views
0

我有一個簡單的應用程序,當你點擊一個圖像從一個GridView時,一個對話框彈出一個按鈕,說視圖。當您點擊按鈕時,我想查看已在圖庫中點擊的圖像。我可以在沒有任何問題的情況下導航到圖庫的情況下正常工作,但我需要它首先點擊對話框。我的LogCat返回錯誤Java.lang.ArrayIndexOutOfBoundsExeption。任何想法,我需要做什麼來獲得這個查看圖像?以下是我的代碼。Android ArrayIndexOutOfBoundsExeption當查看圖像

public void onClick(View v) { 
    Dialog dia = new Dialog(ViewGrid.this); 
    dia.setContentView(R.layout.viewimage); 
    dia.setTitle("What To Do?"); 
    dia.show(); 
    dia.setCancelable(true); 
    Button button = (Button)dia.findViewById(R.id.viewImage); 
    button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     int id = v.getId(); 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW);  
     intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); 
     startActivity(intent); 
    } 
    }); 
} 

我收到這條線上的錯誤:

intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); 

我想通了!下面的代碼是工作代碼。我需要將我從視圖中獲得的我的ID設置爲點擊圖像,而不是點擊查看按鈕。感謝大家的幫助!

public void onClick(final View viewIt) { 
    Dialog dia = new Dialog(ViewGrid.this); 
    dia.setContentView(R.layout.viewimage); 
    dia.setTitle("What To Do?"); 
    dia.show(); 
    dia.setCancelable(true); 
    Button button = (Button)dia.findViewById(R.id.viewImage); 
    button.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     int id = viewIt.getId(); 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW);  
     intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); 
     startActivity(intent); 
    } 
    }); 
} 
+0

whats arrpath .. –

+0

它是一個字符串數組。對不起,我將編輯 – BossWalrus

+1

爲什麼你使用View id作爲數組的索引? –

回答

0

我想通了這個問題,並張貼了我正確的代碼上面的問題。感謝所有幫助過我的人。

0

不知道,但我相信dia.show();應該在最後被調用 - 即後view.setOnClickListener

+0

我將其移至下方,但沒有更改任何內容。我得到的錯誤是在這一行:intent.setDataAndType(Uri.parse(「file://」+ arrPath [id]),「image/*」); – BossWalrus

+0

然後也許[這](http://stackoverflow.com/a/11088980/966550)可能會幫助你 – waqaslam

0

從哪裏引發此ArrayIndexOutOfBoundsException?我假設你在訪問arrPath [i]時拋出它。你如何看待視圖的id應該是arrPath的索引?這似乎是問題所在。

+0

正確,我得到的錯誤是在這一行:intent.setDataAndType(Uri.parse(「file:/ /「+ arrPath [id]),」image/*「); – BossWalrus

+0

你如何看待視圖的id應該是arrPath的索引?這是你的問題所在。您正在使用數組中不存在的索引訪問數組。你怎麼把你的圖片放入這個數組中?我不認爲你用來索引你的數組的id是否會包含任何東西。 –

0

嘗試打印

arrPath[id] 

你應該得到同樣的錯誤。 正如Sky Kelsey所說,視圖id是數組的索引是有問題的。

如果文件路徑例如

File file = new File("/sdcard/yourfile-VIEWID.jpg"); 

然後嘗試,而不是使用數組保存的路徑,做一些

File file = new File("/sdcard/yourfile-"+String.valueof(id)+".jpg");