2013-02-14 47 views
0

在我的android應用程序中我收到了致命的異常。我列出了圖像信息,如縮略圖,圖像名稱和圖像大小。 但是當我試圖執行代碼下面的例外。 這裏是代碼 -列出圖像縮略圖在android中的圖像名稱和大小

public class ImportFile<Context> extends ListActivity { 
String names[]; 
Bitmap bb[]; 
long size1[]; 
Intent i; 
    private String myFile; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mylist); 

     File file = new File(Environment.getExternalStorageDirectory() 
      + File.separator 
      + "gallery" //folder name 
     ); 
     if (!file.exists()) { 
      file.mkdirs(); 
     } 

     setListAdapter(new IconicAdapter()); 

    } 

    class IconicAdapter extends ArrayAdapter<String> { 
     IconicAdapter() { 
      super(ImportFile.this, R.layout.row, R.id.lable, names); 
     } 

     public View getView(int position,View convertview,ViewGroup parent){ 
      View row=super.getView(position, convertview, parent); 
      ImageView icon=(ImageView)row.findViewById(R.id.icon); 

     icon.setImageBitmap(bb[position]); 
     TextView size=(TextView) row.findViewById(R.id.size); 

     size.setText(size1[position]+""); 
     return row; 
     } 

    } 

    //read from sdcard 
    private void getSD() { 
     List<String> item = new ArrayList<String>(); 
     File f = new File("/sdcard/download"); 
     File[] files = f.listFiles(); 

     int j=0; 
     for(int i = 0; i < files.length; i++) { 
      File file = files[i]; 
      //take the file name only 
      long size = file.length()/1024; 

      myFile = file.getPath().substring(file.getPath().lastIndexOf("/")+1,file.getPath().length()).toLowerCase(); 

      if(myFile.endsWith(".jpeg")||myFile.endsWith(".png")|| myFile.endsWith(".gif") || myFile.endsWith(".jpg")) 
      { 
       names[j]=myFile; 

       size1[j]=size; 

       Bitmap b=ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(file.getPath()), 50,50); 
       bb[j]=b; 
       j++; 
      item.add(b +myFile+"    "+"Size:"+size+" KB"); 

      } 

     } 

    } 
    } 
下面

是例外 -

02-14 18:00:44.267: E/AndroidRuntime(909): FATAL EXCEPTION: main 
02-14 18:00:44.267: E/AndroidRuntime(909): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.zproject/com.my.zproject.ImportFile}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.os.Looper.loop(Looper.java:123) 
02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-1 4 18:00:44.267: E/AndroidRuntime(909):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-14 18:00:44.267: E/AndroidRuntime(909): at java.lang.reflect.Method.invoke(Method.java:521) 
02-14 18:00:44.267: E/AndroidRuntime(909): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-14 18:00:44.267: E/AndroidRuntime(909): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-14 18:00:44.267: E/AndroidRuntime(909): at dalvik.system.NativeStart.main(Native Method) 
    02-14 18:00:44.267: E/AndroidRuntime(909): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 

02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.ListActivity.onContentChanged(ListActivity.java:245) 
    02-14 18:00:44.267: E/AndroidRuntime(909): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201) 
    02-14 18:00:44.267: E/AndroidRuntime(909): at android.app.Activity.setContentView(Activity.java:1647) 
    02-14 18:00:44.267: E/AndroidRuntime(909): at com.my.zproject.ImportFile.onCreate(ImportFile.java:33) 

這裏是row.xml-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 


<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:padding="2dip" 

    /> 

<LinearLayout 
    android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
    > 
    <TextView 
     android:id="@+id/lable" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     /> 
    <TextView 
     android:id="@+id/size" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 
    </LinearLayout> 

這裏mylist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ListView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/list" > 
    </ListView> 
    </LinearLayout> 

回答

1

嘗試改變您的ListView的ID到@android:id/list

+0

現在它給出的異常像 - '02 -14 18:51:56.092:E/AndroidRuntime(969):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.my.zproject/com.my.zproject。的importfile}:顯示java.lang.NullPointerException 02-14 18:51:56.092:E/AndroidRuntime(969):\t在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 「 – 2013-02-14 13:26:06

+0

他的答案是正確的它解決了你提出問題的問題。這是一個不同的問題。 – Budius 2013-02-14 13:35:20