如何解决日志猫错误?项目图片查看示例

发布于 2024-11-04 05:27:35 字数 9070 浏览 0 评论 0 原文

您好,现在尝试在我的画廊中创建文件夹......通常我的网格视图单独显示图像,但我需要在我的画廊中创建文件夹。所以,我正在使用文件类...如果我运行我的项目日志 cat 指示错误如何解决这些错误?请检查我的编码......

ImageViewExample.java

package ImageViewExample.ImageViewExample;

import java.io.File;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class ImageViewExample extends Activity {
  /** Called when the activity is first created. */
  private Cursor imagecursor, actualimagecursor;
  private int image_column_index, actual_image_column_index;
  GridView imagegrid;
  private int count;
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      init_phone_image_grid();
}
private void init_phone_image_grid() {
      String[] img = { MediaStore.Images.Thumbnails._ID };
      imagecursor = managedQuery(
      MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
      null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
      image_column_index = imagecursor
     .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
      count = imagecursor.getCount();
      imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
      imagegrid.setAdapter(new ImAdapterh(this));
      imagegrid.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v,
      int position, long id) {
                  System.gc();
                  String[] proj = { MediaStore.Images.Media.DATA };
                  actualimagecursor = managedQuery(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
      null, null, null);
                  actual_image_column_index = actualimagecursor
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                  actualimagecursor.moveToPosition(position);
                  String i = actualimagecursor.getString(actual_image_column_index);
                  System.gc();
                  Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                  intent.putExtra("filename", i);
                  startActivity(intent);
             }
       });
      }


      public class ImAdapterh extends BaseAdapter{

      File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");

      int count=dir.list().length; 

      String[] fileNames = dir.list();

      private Context mContext;

      public ImAdapterh(Context c) {
       mContext = c;
       }

   public int getCount() {
       return count;
   }

   public Object getItem(int position) {
       return null;
   }

   public long getItemId(int position) {
       return 0;
   }

   // create a new ImageView for each item referenced by the Adapter
   public View getView(int position, View convertView, ViewGroup parent) {
       ImageView imageView = null;        


     for(String bitmapFileName : fileNames)
     {
          if (convertView == null) 
          {  // if it's not recycled, initialize some attributes
              imageView = new ImageView(mContext);
              imageView.setLayoutParams(new Gallery.LayoutParams(85, 85));
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
              imageView.setPadding(8, 8, 8, 8);                   

              Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" +  
              bitmapFileName);
              System.out.println(dir);
              imageView.setImageBitmap(bmp);  
           }else 
           {
           imageView = (ImageView) convertView;            

           }
   } 
       return imageView;
       }
      }}

Logcat 错误:

04-29 14:56:23.011: DEBUG/AndroidRuntime(415): >>>>>>>>>>>>>> AndroidRuntime START 
<<<<<<<<<<<<<<
04-29 14:56:23.021: DEBUG/AndroidRuntime(415): CheckJNI is ON
04-29 14:56:23.402: DEBUG/AndroidRuntime(415): --- registering native functions ---
04-29 14:56:24.781: DEBUG/AndroidRuntime(415): Shutting down VM
04-29 14:56:24.791: DEBUG/dalvikvm(415): Debugger has detached; object registry had 1 
entries
04-29 14:56:24.821: INFO/AndroidRuntime(415): NOTE: attach of thread 'Binder Thread 
#3' failed
04-29 14:56:25.761: DEBUG/AndroidRuntime(423): >>>>>>>>>>>>>> AndroidRuntime START 
<<<<<<<<<<<<<<
04-29 14:56:25.761: DEBUG/AndroidRuntime(423): CheckJNI is ON
04-29 14:56:26.141: DEBUG/AndroidRuntime(423): --- registering native functions ---
04-29 14:56:27.552: INFO/ActivityManager(66): Starting activity: Intent { 
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000   
cmp=ImageViewExample.ImageViewExample/.ImageViewExample }
04-29 14:56:28.182: DEBUG/AndroidRuntime(423): Shutting down VM
04-29 14:56:28.232: DEBUG/dalvikvm(423): Debugger has detached; object registry had 1 
entries
04-29 14:56:28.291: INFO/ActivityManager(66): Start proc 
ImageViewExample.ImageViewExample for activity   
ImageViewExample.ImageViewExample/.ImageViewExample: pid=430 uid=10050 gids={}
04-29 14:56:28.312: INFO/AndroidRuntime(423): NOTE: attach of thread 'Binder Thread 
#3' failed
04-29 14:56:29.641: DEBUG/AndroidRuntime(430): Shutting down VM
04-29 14:56:29.641: WARN/dalvikvm(430): threadid=1: thread exiting with uncaught 
exception (group=0x4001d800)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): FATAL EXCEPTION: main
04-29 14:56:29.711: ERROR/AndroidRuntime(430): java.lang.RuntimeException: Unable to   
start activity  
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample.
ImageViewExample.ImageViewExample}: java.lang.NullPointerException
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at  
android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at  
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.os.Looper.loop(Looper.java:123)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.app.ActivityThread.main(ActivityThread.java:4627)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
java.lang.reflect.Method.invokeNative(Native Method)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at   
java.lang.reflect.Method.invoke(Method.java:521)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
dalvik.system.NativeStart.main(Native Method)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): Caused by: 
java.lang.NullPointerException
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
ImageViewExample.ImageViewExample.ImageViewExample$ImAdapterh.<init>  
(ImageViewExample.java:73)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at     
 ImageViewExample.ImageViewExample.ImageViewExample.init_phone_image_grid
(ImageViewExample.java:47)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at   
ImageViewExample.ImageViewExample.ImageViewExample.onCreate
(ImageViewExample.java:36)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at     
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2627)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     ... 11 more
04-29 14:56:29.751: WARN/ActivityManager(66):   Force finishing activity 
ImageViewExample.ImageViewExample/.ImageViewExample
04-29 14:56:30.292: WARN/ActivityManager(66): Activity pause timeout for 
HistoryRecord{4406aab0 ImageViewExample.ImageViewExample/.ImageViewExample}
04-29 14:56:36.701: DEBUG/dalvikvm(189): GC_EXPLICIT freed 162 objects / 7136 bytes in 
121ms
04-29 14:56:41.568: WARN/ActivityManager(66): Activity destroy timeout for 
HistoryRecord{4406aab0 ImageViewExample.ImageViewExample/.ImageViewExample}

请帮助我......

Hi Now trying to create folder in my gallery....normally my grid view display images in separately but I need folder in my gallery. so, I am using file class....if i run my project log cat indicate error how to solve these error? please check my coding also.....

ImageViewExample.java

package ImageViewExample.ImageViewExample;

import java.io.File;
import android.R.string;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class ImageViewExample extends Activity {
  /** Called when the activity is first created. */
  private Cursor imagecursor, actualimagecursor;
  private int image_column_index, actual_image_column_index;
  GridView imagegrid;
  private int count;
@Override
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      init_phone_image_grid();
}
private void init_phone_image_grid() {
      String[] img = { MediaStore.Images.Thumbnails._ID };
      imagecursor = managedQuery(
      MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
      null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
      image_column_index = imagecursor
     .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
      count = imagecursor.getCount();
      imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
      imagegrid.setAdapter(new ImAdapterh(this));
      imagegrid.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v,
      int position, long id) {
                  System.gc();
                  String[] proj = { MediaStore.Images.Media.DATA };
                  actualimagecursor = managedQuery(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
      null, null, null);
                  actual_image_column_index = actualimagecursor
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                  actualimagecursor.moveToPosition(position);
                  String i = actualimagecursor.getString(actual_image_column_index);
                  System.gc();
                  Intent intent = new Intent(getApplicationContext(), ViewImage.class);
                  intent.putExtra("filename", i);
                  startActivity(intent);
             }
       });
      }


      public class ImAdapterh extends BaseAdapter{

      File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");

      int count=dir.list().length; 

      String[] fileNames = dir.list();

      private Context mContext;

      public ImAdapterh(Context c) {
       mContext = c;
       }

   public int getCount() {
       return count;
   }

   public Object getItem(int position) {
       return null;
   }

   public long getItemId(int position) {
       return 0;
   }

   // create a new ImageView for each item referenced by the Adapter
   public View getView(int position, View convertView, ViewGroup parent) {
       ImageView imageView = null;        


     for(String bitmapFileName : fileNames)
     {
          if (convertView == null) 
          {  // if it's not recycled, initialize some attributes
              imageView = new ImageView(mContext);
              imageView.setLayoutParams(new Gallery.LayoutParams(85, 85));
              imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
              imageView.setPadding(8, 8, 8, 8);                   

              Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" +  
              bitmapFileName);
              System.out.println(dir);
              imageView.setImageBitmap(bmp);  
           }else 
           {
           imageView = (ImageView) convertView;            

           }
   } 
       return imageView;
       }
      }}

Logcat Error:

04-29 14:56:23.011: DEBUG/AndroidRuntime(415): >>>>>>>>>>>>>> AndroidRuntime START 
<<<<<<<<<<<<<<
04-29 14:56:23.021: DEBUG/AndroidRuntime(415): CheckJNI is ON
04-29 14:56:23.402: DEBUG/AndroidRuntime(415): --- registering native functions ---
04-29 14:56:24.781: DEBUG/AndroidRuntime(415): Shutting down VM
04-29 14:56:24.791: DEBUG/dalvikvm(415): Debugger has detached; object registry had 1 
entries
04-29 14:56:24.821: INFO/AndroidRuntime(415): NOTE: attach of thread 'Binder Thread 
#3' failed
04-29 14:56:25.761: DEBUG/AndroidRuntime(423): >>>>>>>>>>>>>> AndroidRuntime START 
<<<<<<<<<<<<<<
04-29 14:56:25.761: DEBUG/AndroidRuntime(423): CheckJNI is ON
04-29 14:56:26.141: DEBUG/AndroidRuntime(423): --- registering native functions ---
04-29 14:56:27.552: INFO/ActivityManager(66): Starting activity: Intent { 
act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000   
cmp=ImageViewExample.ImageViewExample/.ImageViewExample }
04-29 14:56:28.182: DEBUG/AndroidRuntime(423): Shutting down VM
04-29 14:56:28.232: DEBUG/dalvikvm(423): Debugger has detached; object registry had 1 
entries
04-29 14:56:28.291: INFO/ActivityManager(66): Start proc 
ImageViewExample.ImageViewExample for activity   
ImageViewExample.ImageViewExample/.ImageViewExample: pid=430 uid=10050 gids={}
04-29 14:56:28.312: INFO/AndroidRuntime(423): NOTE: attach of thread 'Binder Thread 
#3' failed
04-29 14:56:29.641: DEBUG/AndroidRuntime(430): Shutting down VM
04-29 14:56:29.641: WARN/dalvikvm(430): threadid=1: thread exiting with uncaught 
exception (group=0x4001d800)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): FATAL EXCEPTION: main
04-29 14:56:29.711: ERROR/AndroidRuntime(430): java.lang.RuntimeException: Unable to   
start activity  
ComponentInfo{ImageViewExample.ImageViewExample/ImageViewExample.
ImageViewExample.ImageViewExample}: java.lang.NullPointerException
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at  
android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at  
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.os.Handler.dispatchMessage(Handler.java:99)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.os.Looper.loop(Looper.java:123)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.app.ActivityThread.main(ActivityThread.java:4627)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
java.lang.reflect.Method.invokeNative(Native Method)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at   
java.lang.reflect.Method.invoke(Method.java:521)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at   
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
dalvik.system.NativeStart.main(Native Method)
04-29 14:56:29.711: ERROR/AndroidRuntime(430): Caused by: 
java.lang.NullPointerException
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
ImageViewExample.ImageViewExample.ImageViewExample$ImAdapterh.<init>  
(ImageViewExample.java:73)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at     
 ImageViewExample.ImageViewExample.ImageViewExample.init_phone_image_grid
(ImageViewExample.java:47)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at   
ImageViewExample.ImageViewExample.ImageViewExample.onCreate
(ImageViewExample.java:36)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at 
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     at     
android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:2627)
04-29 14:56:29.711: ERROR/AndroidRuntime(430):     ... 11 more
04-29 14:56:29.751: WARN/ActivityManager(66):   Force finishing activity 
ImageViewExample.ImageViewExample/.ImageViewExample
04-29 14:56:30.292: WARN/ActivityManager(66): Activity pause timeout for 
HistoryRecord{4406aab0 ImageViewExample.ImageViewExample/.ImageViewExample}
04-29 14:56:36.701: DEBUG/dalvikvm(189): GC_EXPLICIT freed 162 objects / 7136 bytes in 
121ms
04-29 14:56:41.568: WARN/ActivityManager(66): Activity destroy timeout for 
HistoryRecord{4406aab0 ImageViewExample.ImageViewExample/.ImageViewExample}

please help me.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-11-11 05:27:35

问题几乎肯定是这一行:

int count=dir.list().length;

证明它必须是该行的逻辑链如下:

  • 创建 File 对象的语句不能在 中抛出异常code>ImAdapterh 构造函数本身
  • 这意味着 dir 必须为非空。
  • 这意味着 dir.list() 不会在 ImAdapterh 构造函数中抛出异常。
  • 这只是让 String[] 引用的解引用来获取 length 值成为罪魁祸首。

最后,空指针可以通过以下事实来解释:如果文件(在本例中为 dir)不存在,则 File.list() 返回 null不存在或不表示目录...根据 File.list() javadoc

The problem is almost certainly this line:

int count=dir.list().length;

The chain of logic that demonstrates that it has to be that line is follows:

  • The statement that creates the File object can't throw an exception in the ImAdapterh constructor itself.
  • This means that dir must be non-null.
  • This means that dir.list() won't throw an exception in the ImAdapterh constructor.
  • This just leaves the dereferencing of the String[] reference to get the length value as the culprit.

Finally, the null pointer can be explained by the fact that File.list() returns null if the File (in this case dir) does not exists or doesn't denote a directory ... as per the File.list() javadoc.

一场春暖 2024-11-11 05:27:35

适配器类中出现空指针异常。

File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");

      int count=dir.list().length; 

      String[] fileNames = dir.list();

检查上面的行...返回 null 的值。

Null Pointer exception in your adapter class.

File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");

      int count=dir.list().length; 

      String[] fileNames = dir.list();

check above lines...that a value returning null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文