gpt4 book ai didi

java - Android 应用程序错误 NullPointerException?

转载 作者:行者123 更新时间:2023-12-02 05:31:54 25 4
gpt4 key购买 nike

我正在尝试制作一个Android应用程序,当单击按钮时,它会进入手机目录并可以打开文件。这是我到目前为止的代码:

package com.open1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;



public class MainActivity extends Activity {



@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}



@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

Button button = (Button) findViewById(R.id.button1);



button.setOnClickListener(new OnClickListener() {



@Override

public void onClick(View view) {

Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();

// get the files directory
File lister = this.getFilesDir();

FileInputStream inputStream = null;

byte[] bytes = new byte[500];

int fileIdx = -1;

for (String list : lister.list()){
fileIdx++;
if(list.endsWith("ddd")){
File file = lister.listFiles()[fileIdx];
try {
inputStream = new FileInputStream(file);
bytes = new byte[inputStream.available()];
inputStream.read(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();}
finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

}

private File getFilesDir() {
// TODO Auto-generated method stub
return null;
}



});

return true;

}



}

我可以看到错误,但不知道如何修复它们。这是日志猫:

08-22 12:54:34.447: E/Trace(22072): error opening trace file: No such file or directory (2)
08-22 12:54:34.778: D/libEGL(22072): loaded /system/lib/egl/libEGL_mali.so
08-22 12:54:34.778: D/libEGL(22072): loaded /system/lib/egl/libGLESv1_CM_mali.so
08-22 12:54:34.788: D/libEGL(22072): loaded /system/lib/egl/libGLESv2_mali.so
08-22 12:54:34.828: D/OpenGLRenderer(22072): Enabling debug mode 0
08-22 13:01:03.667: E/Trace(22765): error opening trace file: No such file or directory (2)
08-22 13:01:03.817: D/libEGL(22765): loaded /system/lib/egl/libEGL_mali.so
08-22 13:01:03.837: D/libEGL(22765): loaded /system/lib/egl/libGLESv1_CM_mali.so
08-22 13:01:03.837: D/libEGL(22765): loaded /system/lib/egl/libGLESv2_mali.so
08-22 13:01:03.887: D/OpenGLRenderer(22765): Enabling debug mode 0
08-22 13:01:05.699: D/AndroidRuntime(22765): Shutting down VM
08-22 13:01:05.699: W/dalvikvm(22765): threadid=1: thread exiting with uncaught exception (group=0x417de318)
08-22 13:01:05.709: E/AndroidRuntime(22765): FATAL EXCEPTION: main
08-22 13:01:05.709: E/AndroidRuntime(22765): java.lang.NullPointerException
08-22 13:01:05.709: E/AndroidRuntime(22765): at com.open1.MainActivity$1.onClick(MainActivity.java:65)
08-22 13:01:05.709: E/AndroidRuntime(22765): at android.view.View.performClick(View.java:4103)
08-22 13:01:05.709: E/AndroidRuntime(22765): at android.view.View$PerformClick.run(View.java:17117)
08-22 13:01:05.709: E/AndroidRuntime(22765): at android.os.Handler.handleCallback(Handler.java:615)
08-22 13:01:05.709: E/AndroidRuntime(22765): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 13:01:05.709: E/AndroidRuntime(22765): at android.os.Looper.loop(Looper.java:137)
08-22 13:01:05.709: E/AndroidRuntime(22765): at android.app.ActivityThread.main(ActivityThread.java:4744)
08-22 13:01:05.709: E/AndroidRuntime(22765): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:01:05.709: E/AndroidRuntime(22765): at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:01:05.709: E/AndroidRuntime(22765): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-22 13:01:05.709: E/AndroidRuntime(22765): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 13:01:05.709: E/AndroidRuntime(22765): at dalvik.system.NativeStart.main(Native Method)
08-22 13:01:08.722: I/Process(22765): Sending signal. PID: 22765 SIG: 9

最佳答案

你的方法

        private File getFilesDir() {
// TODO Auto-generated method stub
return null;
}

返回null,其返回值随后在此处使用:

            File lister = this.getFilesDir();
...
>>>> for (String list : lister.list()){

您显然正在引用一个已使用 null 初始化的对象(此处:lister)。

愿意使用调试器和/或处理您的 TODO 注释吗?

关于java - Android 应用程序错误 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25449352/

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