gpt4 book ai didi

java - 应用程序只是强制关闭并停止工作,为什么?

转载 作者:行者123 更新时间:2023-12-01 18:27:32 25 4
gpt4 key购买 nike

我是一名 Android 开发新手,我正在制作或尝试制作一个能够打开几乎所有文件类型的文件浏览器。从音乐文件到视频文件、文档文件等等。但是,我遇到了一个巨大的问题,我的应用程序编译成功但无法打开。这是我的代码:

package com.tstudios.openit;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class MainActivity extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
private ImageView mImageView=(ImageView) findViewById(R.id.gallery);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath = (TextView)findViewById(R.id.path);

root = Environment.getExternalStorageDirectory().getPath();

getDir(root);
}

private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();

if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
String filename = f.getName();
String filenameArray[] = filename.split("\\.");
String extension = filenameArray[filenameArray.length-1];
TextView row=(TextView)findViewById(R.id.rowtext);
//if(extension=="jpg"){
//mImageView.setImageBitmap(BitmapFactory.decodeFile(dirPath));
//}
//if(extension!=null){
//row.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher,0,0,0);
//}

Arrays.sort(files, filecomparator);

for(int i=0; i < files.length; i++)
{
File file = files[i];

if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}

ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}

Comparator<? super File> filecomparator = new Comparator<File>(){

public int compare(File file1, File file2) {

if(file1.isDirectory()){
if (file2.isDirectory()){
return String.valueOf(file1.getName().toLowerCase()).compareTo(file2.getName().toLowerCase());
}else{
return -1;
}
}else {
if (file2.isDirectory()){
return 1;
}else{
return String.valueOf(file1.getName().toLowerCase()).compareTo(file2.getName().toLowerCase());
}
}

}
};


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
String filename = file.getName();
String filenameArray[] = filename.split("\\.");
String extension = filenameArray[filenameArray.length-1];

if (file.isDirectory())
{
if(extension=="jpg"){

mImageView.setImageBitmap(BitmapFactory.decodeFile(filename));
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();

}

}

}

这是我启动应用程序时得到的 logcat,顺便说一句,我正在使用索尼 Xperia Z1 Compact,如果有帮助的话

08-28 12:10:54.779    4169-4169/com.tstudios.openit D/AndroidRuntime﹕ Shutting down VM
08-28 12:10:54.779 4169-4169/com.tstudios.openit W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4164cd88)
08-28 12:10:54.789 4169-4169/com.tstudios.openit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tstudios.openit, PID: 4169
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tstudios.openit/com.tstudios.openit.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5135)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.tstudios.openit.MainActivity.<init>(MainActivity.java:25)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:212)
            at android.app.ActivityThread.main(ActivityThread.java:5135)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
         at dalvik.system.NativeStart.main(Native Method)

还有一个问题,如何将图标放在文件名之前?取决于扩展名非常感谢您的帮助

最佳答案

移动

mImageView=(ImageView) findViewById(R.id.gallery);

setContentView()之后的onCreate(),只保留类级别的成员变量声明:

private ImageView mImageView;

onCreate() 之前, Activity 还没有窗口,您将获得 NPE。

setContentView() 之前,没有可供搜索的 View 层次结构,并且返回 null。

关于java - 应用程序只是强制关闭并停止工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25546781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com