0

主.java

public class Main extends Activity implements onClickListener{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    Toast.makeText(this ,testingShow(), Toast.LENGTH_SHORT).show(); //*1
    ...
    /* //*2
     *  MyProcessData hey = new MyProcessData();
     *  String yo = hey.sameTestingShow();
     *  Toast.makeText(this,yo, Toast.LENGTH_SHORT).show(); 
     */
}
public String testingShow(){
     StringBuilder appVariableDataToInitialize = new StringBuilder();
        BufferedReader reader=null;
        String line = "";
        try {
            reader = new BufferedReader(new FileReader(
                    new File(getFilesDir()+File.separator + DATA_FILE)));
            while ( (line = reader.readLine() )!= null ){
                appVariableDataToInitialize.append(line + "\n");
            }
        reader.close();
            
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "nofile" , Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            // 
            e.printStackTrace();
        }
    
    return appVariableDataToInitialize.toString();
 }

}

MyProcessData.java

public class MyProcessData extends MainActivity{

...

public String sameTestingShow(){
      StringBuilder appVariableDataToInitialize = new StringBuilder();
        BufferedReader reader=null;
        String line = "";
        try {
            reader = new BufferedReader(new FileReader(
                    new File(getFilesDir()+File.separator + DATA_FILE)));
            while ( (line = reader.readLine() )!= null ){
                appVariableDataToInitialize.append(line + "\n");
            }
        reader.close();
            
        } catch (FileNotFoundException e) {
            Toast.makeText(this, "nofile" , Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (IOException e) {
            // 
            e.printStackTrace();
        }
    
    return appVariableDataToInitialize.toString();
 }

}

日志猫

08-30 13:21:00.245: D/AndroidRuntime(2112): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
08-30 13:21:00.245: D/AndroidRuntime(2112): CheckJNI is ON
08-30 13:21:00.325: D/AndroidRuntime(2112): --- registering native functions ---
08-30 13:21:00.675: D/AndroidRuntime(2112): Shutting down VM
08-30 13:21:00.675: D/dalvikvm(2112): Debugger has detached; object registry had 1 entries
08-30 13:21:00.685: I/AndroidRuntime(2112): NOTE: attach of thread 'Binder Thread #3' failed
08-30 13:21:00.995: D/AndroidRuntime(2120): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
08-30 13:21:00.995: D/AndroidRuntime(2120): CheckJNI is ON
08-30 13:21:01.075: D/AndroidRuntime(2120): --- registering native functions ---
08-30 13:21:01.425: I/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.easylog/.MainActivity }
08-30 13:21:01.436: I/ActivityManager(58): Start proc com.easylog for activity com.easylog/.MainActivity: pid=2126 uid=10040 gids={}
08-30 13:21:01.465: D/AndroidRuntime(2120): Shutting down VM
08-30 13:21:01.475: D/dalvikvm(2120): Debugger has detached; object registry had 1 entries
08-30 13:21:01.955: I/global(2126): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
08-30 13:21:01.995: D/AndroidRuntime(2126): Shutting down VM
08-30 13:21:01.995: W/dalvikvm(2126): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-30 13:21:02.025: E/AndroidRuntime(2126): FATAL EXCEPTION: main
08-30 13:21:02.025: E/AndroidRuntime(2126): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.easylog/com.easylog.MainActivity}: java.lang.NullPointerException
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.os.Looper.loop(Looper.java:123)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invoke(Method.java:521)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at dalvik.system.NativeStart.main(Native Method)
08-30 13:21:02.025: E/AndroidRuntime(2126): Caused by: java.lang.NullPointerException
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.easylog.MyProcessData.sameTestingShow(MyProcessData.java:56)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at com.easylog.MainActivity.onCreate(MainActivity.java:54)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-30 13:21:02.025: E/AndroidRuntime(2126):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-30 13:21:02.025: E/AndroidRuntime(2126):     ... 11 more
08-30 13:21:02.025: W/ActivityManager(58):   Force finishing activity com.easylog/.MainActivity
08-30 13:21:02.535: W/ActivityManager(58): Activity pause timeout for HistoryRecord{450a1690 com.easylog/.MainActivity}
08-30 13:21:04.235: I/Process(2126): Sending signal. PID: 2126 SIG: 9
08-30 13:21:04.255: I/ActivityManager(58): Process com.easylog (pid 2126) has died.
08-30 13:21:04.265: W/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@45058308
08-30 13:21:12.781: W/ActivityManager(58): Activity destroy timeout for HistoryRecord{450a1690 com.easylog/.MainActivity}
08-30 13:22:54.366: D/dalvikvm(171): GC_EXPLICIT freed 153 objects / 6128 bytes in 97ms

当我运行这段代码时,它完美地工作。
但是在我在 Main.java 中注释 *1 并且在 Main.java 中也取消注释 *2 之后,我得到一个错误 java.lang.NullPointerException 。
这是什么意思?它是完全相同的功能(相同的代码),所以我不知道问题出在哪里。我是编程初学者,请温柔。

编辑 => 已解决

1.将传递上下文的构造函数添加到MyProcessData.java

private final Context myC;

public MyProcessData(Context c) {
    myContext = c;
}

2.edit MyProcessData => sameTestingShow()

reader = new BufferedReader(new FileReader(
                new File(myContext.getFilesDir()+File.separator + DATA_FILE)));
4

2 回答 2

0

要从另一个类调用方法,您应该这样做:ClassName.MethodName(),而不是:

MyProcessData hey = new MyProcessData();
String yo = hey.sameTestingShow();

更新代码

String yo = MyProcessData.sameTestingShow();
于 2012-08-30T03:45:40.180 回答
0

您不能使用新调用初始化 MyProcessData。任何活动都应该只由框架初始化,以便上下文是正确的。在你的情况下,你不能这样做

MyProcessData hey = new MyProcessData();

调用另一个 Activity 的方法是个坏主意,因为上下文不会被初始化。

如果你非常坚持。

然后像下面一样传递上下文

MyProcessData hey = new MyProcessData(this); 

然后使用此上下文获取目录context.getFileDir()

于 2012-08-30T04:50:40.243 回答