0

我有来自android-coding.blogspot的 JPG 文件选择器

Java - 1.7;安卓 - 2.2;蚀。

package com.androidcustomdialog;

 import java.io.File;

public class AndroidCustomDialogActivity extends Activity {

 Button buttonOpenDialog;
 Button buttonUp;
 TextView textFolder;
 ImageView image;

 String KEY_TEXTPSS = "TEXTPSS";
 static final int CUSTOM_DIALOG_ID = 0;

 ListView dialog_ListView;

 File root;
 File curFolder;

 private List<String> fileList = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        image = (ImageView)findViewById(R.id.image);

        buttonOpenDialog = (Button)findViewById(R.id.opendialog);
        buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View arg0) {
                showDialog(CUSTOM_DIALOG_ID);
            }});

        root = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        curFolder = root;
    }

 @Override
 protected Dialog onCreateDialog(int id) {

  Dialog dialog = null;

  switch(id) {
     case CUSTOM_DIALOG_ID:
      dialog = new Dialog(AndroidCustomDialogActivity.this);
      dialog.setContentView(R.layout.dialoglayout);
      dialog.setTitle("Select JPG");

      dialog.setCancelable(true);
      dialog.setCanceledOnTouchOutside(true);

      textFolder = (TextView)dialog.findViewById(R.id.folder);

      buttonUp = (Button)dialog.findViewById(R.id.up);
      buttonUp.setOnClickListener(new OnClickListener(){
          public void onClick(View v) {
              ListDir(curFolder.getParentFile());
          }});

      //Prepare ListView in dialog
      dialog_ListView = (ListView)dialog.findViewById(R.id.dialoglist);
      dialog_ListView.setOnItemClickListener(new OnItemClickListener(){
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
              File selected = new File(fileList.get(position));
              if(selected.isDirectory()){
                  ListDir(selected); 
              }else {
                  Toast.makeText(AndroidCustomDialogActivity.this,
                          selected.toString() + " selected",
                          Toast.LENGTH_LONG).show();
                  dismissDialog(CUSTOM_DIALOG_ID);

                  Bitmap bm = BitmapFactory.decodeFile(selected.getAbsolutePath());
                  image.setImageBitmap(bm);
        }}});
         break;
     }
  return dialog;
 }

 @Override
 protected void onPrepareDialog(int id, Dialog dialog, Bundle bundle) 
 {
  super.onPrepareDialog(id, dialog, bundle);
  switch(id) {
     case CUSTOM_DIALOG_ID:
      ListDir(curFolder);
         break;
     } 
 }

 void ListDir(File f)
 {
  if(f.equals(root)){
   buttonUp.setEnabled(false);
  }else{                            //115 134 54
   buttonUp.setEnabled(true);
  }

  curFolder = f;
  textFolder.setText(f.getPath());

  File[] files = f.listFiles();
  fileList.clear();
  for (File file : files)
  {  
       if(file.isDirectory()){
        fileList.add(file.getPath());
       }else{
        Uri selectedUri = Uri.fromFile(file);
        String fileExtension
         = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString());
        if(fileExtension.equalsIgnoreCase("jpg")){
         fileList.add(file.getPath());
        }
       }
  }

  ArrayAdapter<String> directoryList
   = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList);
  dialog_ListView.setAdapter(directoryList);
 }

启动前没有错误,但按“加载JPG”后却“意外停止”。日志是:

07-24 07:43:10.328: E/AndroidRuntime(5634): FATAL EXCEPTION: main 
07-24 07:43:10.328: E/AndroidRuntime(5634): java.lang.NullPointerException 
07-24 07:43:10.328: E/AndroidRuntime(5634): at com.androidcustomdialog.AndroidCustomDialogActivity.ListDir(AndroidCustomDialogActivity.java:129) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at com.androidcustomdialog.AndroidCustomDialogActivity.onPrepareDialog(AndroidCustomDialogActivity.java:111) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.app.Activity.showDialog(Activity.java:2555) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.app.Activity.showDialog(Activity.java:2514) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at com.androidcustomdialog.AndroidCustomDialogActivity$1.onClick(AndroidCustomDialogActivity.java:54) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.view.View.performClick(View.java:2408) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.view.View$PerformClick.run(View.java:8816) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.os.Handler.handleCallback(Handler.java:587) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.os.Handler.dispatchMessage(Handler.java:92) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.os.Looper.loop(Looper.java:123) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at java.lang.reflect.Method.invokeNative(Native Method) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at java.lang.reflect.Method.invoke(Method.java:521) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-24 07:43:10.328: E/AndroidRuntime(5634): at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

错误清楚地打印在日志中 - 您NullPointerException在第 129 行发生了错误。

但是,这些行与您打印的源不匹配,因此很难准确识别错误。我的猜测是finListDir为空,或者因为不是有效目录f.listFiles()而返回。nullcurFile

于 2012-07-24T08:18:16.073 回答