2011-02-04 83 views
0

我收到一個錯誤,當我點擊我的第一個活動(資源管理器)中的鏈接時,我想調出我創建的文件選擇器(AndroidExplorer)是否有我的代碼活動代碼或我的清單有問題。從活動切換到另一個清單文件問題?

任何幫助,將不勝感激

資源類

public class Explorer extends Activity{ 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button picture = (Button)findViewById(R.id.picture); 

    picture.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(Explorer.this, AndroidExplorer.class); 
      startActivity(intent); 
      finish(); 
     } 

     }); 
    } 
} 

AndroidExplorer類

public class AndroidExplorer extends ListActivity { 

private List<String> item = null; 
private List<String> path = null; 
private String root="/sdcard/DCIM/camera/"; 
private TextView myPath; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main2); 
    myPath = (TextView)findViewById(R.id.path); 
    getDir(root); 
} 

private void getDir(String dirPath) 
{ 
    myPath.setText("Location: " + dirPath); 

    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 

    File f = new File(dirPath); 
    File[] files = f.listFiles(); 

    if(!dirPath.equals(root)) 
    { 

     item.add(root); 
     path.add(root); 

     item.add("../"); 
     path.add(f.getParent()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
      File file = files[i]; 
      path.add(file.getPath()); 
      if(file.isDirectory()) 
       item.add(file.getName() + "/"); 
      else 
       item.add(file.getName()); 
    } 

    ArrayAdapter<String> fileList = 
     new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    File file = new File(path.get(position)); 

    if (file.isDirectory()) 
    { 
     if(file.canRead()) 
      getDir(path.get(position)); 
     else 
     { 
      new AlertDialog.Builder(this) 
      .setIcon(R.drawable.icon) 
      .setTitle("[" + file.getName() + "] folder can't be read!") 
      .setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
         } 
        }).show(); 
     } 
    } 
    else 
    { 
     new AlertDialog.Builder(this) 
      .setIcon(R.drawable.icon) 
      .setTitle("[" + file.getName() + "]") 
      .setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 

         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          // TODO Auto-generated method stub 
         } 
        }).show(); 
    } 
}} 

清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.Explorer" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".Explorer" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
<activity android:name=".AndroidExplorer" 
     android:label="Screen 2 - New actvity."> 

    </activity> 
</application> 


</manifest> 
+1

請你能提供logcat輸出的錯誤嗎? – 2011-02-04 22:05:15

回答

1

嘛,看到你的根變種可以通向SD卡的路徑,如果你沒有插入SD卡,目錄似乎不存在。我認爲你應該捕獲該錯誤並拋出一個彈出窗口或讓用戶知道需要SD卡。

相關問題