我有一个标签视图,其中一个标签中有一个列表视图。此列表视图显示包含图像的文件的名称。单击名称时,我希望它显示图像。我尝试了很多技术,但仍然遇到错误,我不知道该怎么做。如果您能提供任何帮助或信息,我们将不胜感激。
以下是代码:
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)
答案 0 :(得分:0)
尝试调试以查看哪个阶段出错了。它似乎得到一个空指针异常。