0

在我的主要活动中:

count=count+1;
//the problem seems to be in this line of code below
String fileName=f.getFileName(count).toString();
// the method below invokes a method from another class. it works fine so please ignore
helloTxt.setText(readText.readTxt(fileName));

f 是 FileName 类的对象,我从中获取文件名:

public class FileName {

    String file;
    public String getFileName(int count) {
        // TODO Auto-generated method stub

        if (count==1)
        {
            file.equals("textone");
        }

        if(count==2)
        {
            file.equals("texttwo");
        }
        return file;

    }



    }

原木猫:

03-19 12:02:00.554: D/AndroidRuntime(5550): Shutting down VM
03-19 12:02:00.554: W/dalvikvm(5550): threadid=1: thread exiting with uncaught exception (group=0x418342a0)
03-19 12:02:00.562: E/AndroidRuntime(5550): FATAL EXCEPTION: main
03-19 12:02:00.562: E/AndroidRuntime(5550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testflashfile/com.example.testflashfile.MainActivity}: java.lang.NullPointerException
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.os.Looper.loop(Looper.java:137)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.ActivityThread.main(ActivityThread.java:4895)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at java.lang.reflect.Method.invoke(Method.java:511)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at dalvik.system.NativeStart.main(Native Method)
03-19 12:02:00.562: E/AndroidRuntime(5550): Caused by: java.lang.NullPointerException
03-19 12:02:00.562: E/AndroidRuntime(5550):     at com.example.testflashfile.FileName.getFileName(FileName.java:11)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at com.example.testflashfile.MainActivity.onCreate(MainActivity.java:48)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.Activity.performCreate(Activity.java:5163)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-19 12:02:00.562: E/AndroidRuntime(5550):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-19 12:02:00.562: E/AndroidRuntime(5550):     ... 11 more
03-19 12:02:11.164: I/Process(5550): Sending signal. PID: 5550 SIG: 9

所有活动都列在清单中。因此,非常感谢任何可以解决此问题的帮助。

提前致谢。

4

3 回答 3

2

代替

file.equals("textone");

file = "textone";

file.equals("texttwo");

file = "texttwo";

您正在为字符串文件获取 NPE。

于 2013-03-19T06:44:01.563 回答
2

代替

file.equals("textone");
file.equals("texttwo");

利用

file = "textone";
file = "texttwo";

在使用方法之前,您应该采取一些步骤。见API

请参阅方法的文档equals

将此字符串与指定对象进行比较。

它不分配值。分配一个值使用=运算符。

以及为什么抛出NullPointerException

默认情况下,类的实例字段被初始化为其默认值。引用类型分配有 default null。并且在file.equals("textone");执行之前,file变量被分配null并导致NPE。

于 2013-03-19T06:44:32.570 回答
1

这意味着您正在访问一个空值

file.equals("textone");

首先,您需要使用字符串初始化文件

file = "textone";
file = "texttwo";

equals 用于比较而不是用于分配

这里文件为空

于 2013-03-19T06:44:05.263 回答