我有一個簡單的應用程序,當你點擊一個圖像從一個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);
}
});
}
whats arrpath .. –
它是一個字符串數組。對不起,我將編輯 – BossWalrus
爲什麼你使用View id作爲數組的索引? –