0

これらのタブのいずれかにリストビューがあるタブビューがあります。このリストビューには、画像を含むファイルの名前が表示されます。名前をクリックすると画像が表示されるようにしたいです。さまざまな手法を試しましたが、それでもエラーが発生し続け、何をすべきかわかりません。ヘルプや情報を提供していただければ幸いです。

コードは次のとおりです。

package com.example.classname;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class RecordsActivityTwo extends ListActivity {

private static String f2;
private static String fileNames[];
final File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/classname/Camera");
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    //ListDir(root);
    if (root.isDirectory()){
        //lists the file names into an array
        fileNames = root.list();


        //loop used to remove the jpg suffix on file names
        int cntr = 0;
        while (cntr <= (fileNames.length - 1)) {
            fileNames[cntr] = fileNames[cntr].replace(".jpg", "");
            cntr = cntr + 1;
        }

    } else {
        Toast.makeText(this, "Did not get into the directory", Toast.LENGTH_LONG).show();
    }

    setListAdapter(new ArrayAdapter<String>(this, R.layout.recordsactivitytwo, fileNames));






     final ListView lv = getListView();
     lv.setTextFilterEnabled(true);
     lv.setClickable(true);

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

     public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3){

         Object o = lv.getItemAtPosition(position);
         f2 = o.toString() + ".jpg";
         Toast.makeText(getApplicationContext(), f2, Toast.LENGTH_LONG);
         //now write the code for loading the file
         //object o should be the file, so i need to load object o
         //the following code deals with opening the clicked file and displaying it
         imageViewMethod(f2);
         //Intent i = new Intent(getApplicationContext(), ImageViewActivity.class);
         //i.putExtra("text", f2);
         //startActivity(i);
         //ImageView iv = (ImageView)findViewById(R.id.imageviewreader);
         //iv.setImageBitmap(BitmapFactory.decodeFile(f2)); //displays the image from the file to the image view
     }
     });


}

public void imageViewMethod(String file){
    Logger log = Logger.getLogger("com.example.classname");

    try {
    File fileName = new File(root, file);
    String dirFileName = fileName.toString();
    Toast.makeText(getApplicationContext(), dirFileName, Toast.LENGTH_LONG).show();
    ImageView iv = (ImageView)findViewById(R.id.imageviewreader);
    iv.setImageBitmap(BitmapFactory.decodeFile(dirFileName));
    super.setContentView(iv);
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Hit Exception", Toast.LENGTH_LONG).show();
        log.log(Level.SEVERE, "uncaught exception", e);
    }
}

}

logcat の出力は次のとおりです。

06-30 15:59:17.406: E/com.example.ufohunter(569): uncaught exception
06-30 15:59:17.406: E/com.example.ufohunter(569): java.lang.NullPointerException
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.example.ufohunter.RecordsActivityTwo.imageViewMethod(RecordsActivityTwo.java:93)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.example.ufohunter.RecordsActivityTwo$1.onItemClick(RecordsActivityTwo.java:73)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.widget.ListView.performItemClick(ListView.java:3513)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.os.Handler.handleCallback(Handler.java:587)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.os.Handler.dispatchMessage(Handler.java:92)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.os.Looper.loop(Looper.java:130)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at android.app.ActivityThread.main(ActivityThread.java:3683)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at java.lang.reflect.Method.invokeNative(Native Method)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at java.lang.reflect.Method.invoke(Method.java:507)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-30 15:59:17.406: E/com.example.ufohunter(569):   at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

どのフェーズで問題が発生したかを確認するためにデバッグを試みます。nullポインタ例外が発生したようです。

于 2012-06-30T16:22:57.600 に答える