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