1

在我真正开始做一个项目之前,我想练习一下我认为的 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);
        }
    }

}

我也有MainActivity尝试使用的,ChipCount但它崩溃了。

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");
    }

}

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

为什么这会使程序崩溃,我怎么可能修复它。

这是日志猫:

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
4

2 回答 2

2

你永远不会初始化chips. 因此它是null,所以你不能访问它的方法。

而不是ChipCount chips;,尝试:ChipCount chips = new ChipCount();

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

编辑

此外,您永远不会向您的ChipCount对象传递file_context参数。这使得file_context null当你调用它时loadChips();

试试这个:

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");
    }

}

然后,在您的构造函数中ChipCount,接受一个Context参数,然后执行file_context = newContext;.

于 2012-10-02T02:44:42.620 回答
1

您从未初始化ChipCount chips;,因此调用时为 null ,并引发NullPointerException.

你需要ChipCount chips = new ChipCount();在你的代码中有类似的东西。

于 2012-10-02T02:44:26.880 回答