Android 台灣中文網
標題:
寫程式也遇到科學無法解釋的事情
[打印本頁]
作者:
lkk47
時間:
2011-11-21 20:46
標題:
寫程式也遇到科學無法解釋的事情
我把出現問題的程式碼貼出來 全貼可能沒耐心看吧
先說明一下在專案中有3個class 假設a.java b.java c.java a,b,c都是個自的activity
a程式碼片段如下:
private void findfile(){[color=Green]//此方法搜尋sdcard/DCIM/100MEDIA/的所有圖片[/color]
File images = new File("/sdcard/DCIM/100MEDIA/");
File[] imagelist = images.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
[color=Red] isUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
isUrls[i] = Uri.parse(mFiles[i]);
}[/color]
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, final int arg2,
long arg3) {//[color=Green]這方法是Gallery選擇會發生的事件[/color]
// TODO Auto-generated method stub
argg=arg2;//[color=Green]等等c會用到這一個因為argg是公開的[/color]
imageswitch.setImageURI(isUrls[arg2]);[color=Green]//這裡只能給b用給c就發生問題了 這段說明imageswitch顯示Gallery選擇的圖片[/color]
shorturl=isUrls[arg2];
imageswitch.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.setClass(Chosephoto.this, gender.class);
Bundle bundle=new Bundle();
bundle.putInt("int", arg2);
intent.putExtras(bundle);
startActivity(intent);
//使用intent bundle傳送訊息到activity
return false;
}});
}
複製代碼
b程式碼片段如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.boy_image_view);
iv=(ImageView) findViewById(R.id.imageView1);
[color=Red]Bundle bundle =this.getIntent().getExtras();
transfer=bundle.getInt("arg2");
iv.setImageURI(Chosephoto.isUrls[transfer])[/color];
複製代碼
//紅色這段是bundle接收 iv是顯示傳送過來的圖片uri 跟a的imageswitch.setImageURI(isUrls[arg2]);一樣意思
c的程式碼片段如下:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.shorthair_xml);
shortiv1=(ImageView) findViewById(R.id.shortimageView1);
[color=Red]shortiv1.setImageURI(Chosephoto.isUrls[Chosephoto.argg])[/color];[color=Green]//這一段根bundle是一樣的用法 我也試過bundle用法結果都依樣 這一段就跟b的片段iv.setImageURI(Chosephoto.isUrls[transfer])一樣 問題出現在這一句[/color]
複製代碼
輸出結果a,b都正常c出現問題 logcat 顯示c的shortiv1.setImageURI(Chosephoto.isUrls[Chosephoto.argg])這行出現錯誤
最後是很多次 得到一個問題 setImageURI(Chosephoto.isUrls[Chosephoto.argg])這一句只可以給一個activity使用
要是2個activity使用會有1個出錯(換句話說a給b使用成功,b再給c使用c就出錯,a給c也是成功 就是不能給第3個)
不知道是什麼靈異現象 還是有其他方法解決
作者:
ploglin
時間:
2011-11-22 08:55
你把實際錯誤訊息可以貼上來。
我想問題應該跟你所敍述的不太一樣。
作者:
lkk47
時間:
2011-11-22 08:59
ploglin 發表於 2011-11-22 08:55
登錄/註冊後可看大圖
你把實際錯誤訊息可以貼上來。
我想問題應該跟你所敍述的不太一樣。
我的logcat
11-22 08:52:58.181: E/AndroidRuntime(7160): at com.aa.Shorthair.onCreate(Shorthair.java:36)
這一行也就是c片段的shortiv1.setImageURI(Chosephoto.isUrls[Chosephoto.argg])
作者:
ploglin
時間:
2011-11-22 16:11
把所有的堆疊的錯誤的貼出來。
要看是哪一個部份出問題,必需要看完整的訊息。
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1