0

android-coding.blogspotから JPG File Chooser を入手しました。

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

起動前はエラーは出ていませんが、「Load 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が null であるか、有効なディレクトリではないためf.listFiles()に返されていると思われます。nullcurFile

于 2012-07-24T08:18:16.073 に答える