2012-07-23 146 views
0

五月7日至23日:01:10.925:E/AndroidRuntime(4520):致命異常:主 五月7日至23日:01:10.925:E/AndroidRuntime(4520 ): java.lang.RuntimeException:無法實例化活動 ComponentInfo {nsixty.crew.app/nsixty.crew.app.gallery}: java.lang.NullPointerException 07-23 05:01:10.925: E/AndroidRuntime (4520):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586) 07-23 05:01:10.925:E/AndroidRuntime(4520):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java :1680) 07-23 05:01:10.925:E/AndroidRuntime(4520):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)07-23 05:01:10.925:E/AndroidRuntime(4520): at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 07-23 05:01:10.925:E/AndroidRuntime(4520):at android.os.Handler.dispatchMessage(Handler.java:99 )07-23 E/AndroidRuntime(4520):at android.os.Looper.loop(Looper.java:130)07-23 05:01:10.925: E/AndroidRuntime(4520) :at android.app.ActivityThread.main(ActivityThread.java:3703)07-23 05:01:10.925:E/AndroidRuntime(4520):at java.lang.reflect.Method.invokeNative(Native Method)07 -23 05/01/10.925:E/AndroidRuntime(4520):在 java.lang.reflect.Method.invoke(Method.java:507)07-23 05:01:10.925: E/AndroidRuntime(4520):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841) 07-23 05:01:10.925:E/AndroidRuntime(4520):at com.android.internal.os.ZygoteInit。主要(ZygoteInit.java:599)07-23 05:01:10.925:E/AndroidRuntime(4520):at dalvik.system.NativeStart.main(Native Method)07-23 05:01:10.925: E/AndroidRuntime(4520):引起:java.lang.NullPointerException 07-23 05:01:10.925:E/AndroidRuntime(4520):at nsixty.crew.app.gallery。(gallery.java:49)07-23 05:01:10.925: E/AndroidRuntime(4520) :at java.lang.Class.newInstanceImpl(Native Method)07-23 05:01:10.925:E/AndroidRuntime(4520):at java.lang.Class.newInstance(Class.java:1409)07-23 05 :01:10.925: E/AndroidRuntime(4520):at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 07-23 05:01:10.925:E/AndroidRuntime(4520):at android。 app.ActivityThread.performLaunchActivity(ActivityThread.java:1578) 5月7日至23日:01:10.925:E/AndroidRuntime(4520):... 11多個無法實例活動ComponentInfo顯示java.lang.NullPointerException

public class gallery extends ListActivity { 

private List<String> item = null; 

private List<String> path = null; 

private String root = "sdcard/Crews/"; 

private TextView myPath; 
VideoView videoView; 

String[] itemArr; 

String[] itemArr2 = item.toArray(new String[item.size()]); 
ListView list; 

File folder = new File("sdcard/Crews/Videos/"); 

MyThumbnaildapter fileList; 

/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 

    setContentView(R.layout.gallery); 
    myPath = (TextView) findViewById(R.id.path); 

    getDir(root); 

    videoView = (VideoView) this.findViewById(R.id.videoView); 

} 

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); 

     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()); 
     } 

    } 

    fileList = new MyThumbnaildapter(this, R.layout.row, itemArr2); 
    setListAdapter(fileList); 
    list = (ListView) findViewById(android.R.id.list); 

    list.setAdapter(fileList); 
    registerForContextMenu(list); 

} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    final File file = new File(path.get(position)); 

    if (file.isDirectory()) 

    { 

     if (file.canRead()) { 

      getDir(path.get(position)); 
      // imageThumbnail.setImageBitmap(bmThumbnail); 
     } 

     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) 
     // .setView(getCurrentFocus()) 
       .setTitle("[" + file.getName() + "]") 

       .setPositiveButton("OK", 

       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 

         if (file.getPath().contains(".3gp")) { 

          Intent intent = new Intent(); 
          intent.setAction(Intent.ACTION_VIEW); 

          intent.setDataAndType(
            Uri.fromFile(new File(file.getPath())), 
            "video/*"); 

          startActivity(intent); 

         } else if (file.getPath().contains(".jpg")) { 
          Intent intent = new Intent(); 
          intent.setAction(Intent.ACTION_VIEW); 

          intent.setDataAndType(
            Uri.fromFile(new File(file.getPath())), 
            "image/*"); 

          startActivity(intent); 
         } else { 
          Toast displayError = Toast.makeText(
            gallery.this, "unsupported media type", 
            Toast.LENGTH_SHORT); 
          displayError.show(); 
         } 

        } 

       }).show(); 
     // }); 
    } 

} 

}

public class MyThumbnaildapter extends ArrayAdapter<String> { 

    public MyThumbnaildapter(Context context, int textViewResourceId, 
      String[] objects) { 
     super(context, textViewResourceId, objects); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     View row = convertView; 
     if (row == null) { 
      LayoutInflater inflater = getLayoutInflater(); 
      row = inflater.inflate(R.layout.row, parent, false); 
     } 



     ImageView imageThumbnail = (ImageView) row 
       .findViewById(R.id.Thumbnail); 


     Bitmap bmThumbnail; 
     bmThumbnail = ThumbnailUtils.createVideoThumbnail(
       itemArr2[position], Thumbnails.MICRO_KIND); 
     imageThumbnail.setImageBitmap(bmThumbnail); 

     return row; 
    } 

} 
+0

請刪除不相關的問題的任何代碼,並添加一些文字描述你的問題是什麼,並當它發生。 – Keppil 2012-07-23 09:09:07

回答

0

盲目假設,

fileList = new MyThumbnaildapter(this, R.layout.row, itemArr2); 

itemArr2爲空,因爲,你試圖啓動它之前全球填寫項數組.. 一樣,

String[] itemArr2 = item.toArray(new String[item.size()]); 

解決方案(可能):

String[] itemArr2 = item.toArray(new String[item.size()]); 線填充item字符串數組後,

所以應該在這裏,

String[] itemArr2 = item.toArray(new String[item.size()]); 
fileList = new MyThumbnaildapter(this, R.layout.row, itemArr2); 
setListAdapter(fileList); 

也可以刪除這些兩條線,

list = (ListView) findViewById(android.R.id.list); 
list.setAdapter(fileList); 

調試應用程序代碼,並找出問題..

+0

感謝您的修復。 – ImGeorge 2012-07-23 20:32:30

相關問題