在我真正开始从事项目工作之前,我想练习一下我会考虑的android开发基本原理。我目前有一个ChipCount类,该类将负责保存和加载玩家拥有的筹码量。该班级还将负责跟踪一般的筹码数量。

我的ChipCount类当前如下所示:

package com.example.parceltest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class ChipCount {

    private int number_of_chips;
    Context file_context;

    public ChipCount() {
        loadChips();
    }

    public void setChips(int chips) {
        this.number_of_chips = chips;
    }

    public int getChips() {
        return number_of_chips;
    }

    public void saveChips() {
        String filename = "chipsave";
        String save_string = String.valueOf(number_of_chips);

        try {
            FileOutputStream output = file_context.getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
            output.write(save_string.getBytes());
            output.close();
        } catch(IOException e) {

        }

    }

    public void loadChips() {
        String filename = "chipsave";
        String chip_count = "";

        try {
            FileInputStream input = file_context.getApplicationContext().openFileInput(filename);
            input.read(chip_count.getBytes());
        } catch(IOException e) {
            this.setChips(500);
        }
    }

}


我也有尝试使用MainActivityChipCount,但是它崩溃了。

package com.example.parceltest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    ChipCount chips = new ChipCount();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView number_of_chips = (TextView) findViewById(R.id.number_of_chips);
        int chip = chips.getChips();
        //number_of_chips.setText("500");
    }

}


通过注释掉不同的行,我确定问题出在int chip = chips.getChips();行。

为什么这会使程序崩溃,以及如何解决?

这是logcat:

10-01 22:51:06.958: I/Process(28051): Sending signal. PID: 28051 SIG: 9
10-01 22:51:26.669: D/AndroidRuntime(28106): Shutting down VM
10-01 22:51:26.669: W/dalvikvm(28106): threadid=1: thread exiting with uncaught exception (group=0x40b7c300)
10-01 22:51:26.669: E/AndroidRuntime(28106): FATAL EXCEPTION: main
10-01 22:51:26.669: E/AndroidRuntime(28106): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.parceltest/com.example.parceltest.MainActivity}: java.lang.NullPointerException
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.os.Looper.loop(Looper.java:137)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.main(ActivityThread.java:4745)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.reflect.Method.invoke(Method.java:511)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at dalvik.system.NativeStart.main(Native Method)
10-01 22:51:26.669: E/AndroidRuntime(28106): Caused by: java.lang.NullPointerException
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.example.parceltest.ChipCount.loadChips(ChipCount.java:45)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.example.parceltest.ChipCount.<init>(ChipCount.java:15)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.example.parceltest.MainActivity.<init>(MainActivity.java:9)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.Class.newInstanceImpl(Native Method)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.Class.newInstance(Class.java:1319)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-01 22:51:26.669: E/AndroidRuntime(28106):    ... 11 more

最佳答案

您永远不会初始化chips。因此它是null,因此您无法访问其方法。

代替ChipCount chips;,请尝试:ChipCount chips = new ChipCount();

注意:此外,将来当您崩溃时,请发布LogCat错误。在这种情况下,这是一个相对简单的修复程序,但是将来可能需要这些日志。

编辑:

此外,您绝不会向ChipCount对象传递file_context参数。当您在file_context中调用null loadChips();时,即为ChipCount

尝试这个:

public class MainActivity extends Activity {

    ChipCount chips; // Don't initialize here, on second thought

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chips = new ChipCount(this); // Initialize here; and pass it `this`, which is an Activity, and also a Context

        TextView number_of_chips = (TextView) findViewById(R.id.number_of_chips);
        int chip = chips.getChips();
        //number_of_chips.setText("500");
    }

}


然后,在Context的构造函数中,接受file_context = newContext;参数,然后执行。

08-04 20:47