尝试从文件读取数据时发生此致命异常。尝试过我能想到的一切,但没有运气。还查看了谷歌提供的大部分内容。它看起来似乎错误源自我的代码之外的其他地方。
有什么方法可以解决这个问题吗?
文件:
package ee.arvestus.karvastaja;
public class SquareMethodInsertActivity extends Activity {
private static BufferedReader bufferedReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_square_method_insert);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void init() {
InputStream fragInputStream;
try {
fragInputStream = getAssets().open(Constants.PATH_TO_FRAG_DATA);
} catch (IOException e) {
Toast.makeText(SquareMethodInsertActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
bufferedReader = new BufferedReader(new InputStreamReader(fragInputStream));
try {
fragInputStream.close();
} catch (IOException e) {
Toast.makeText(SquareMethodInsertActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
}
public void addTarget(View view) {
}
WeaponSettings[] weaponSettings = new WeaponSettings[3];
try {
if (battery.getCoordinates1() != null) {
weaponSettings[0] = getWeaponSettings(target.getDistances()[0], 4);
}
if (battery.getCoordinates2() != null) {
weaponSettings[1] = getWeaponSettings(target.getDistances()[1], 4);
}
if (battery.getCoordinates3() != null) {
weaponSettings[2] = getWeaponSettings(target.getDistances()[2], 4);
}
} catch (IOException e) {
Toast.makeText(SquareMethodInsertActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
return;
}
target.setWeaponSettings(weaponSettings.clone());
Toast.makeText(SquareMethodInsertActivity.this, Integer.toString(target.getWeaponSettings()[0].getLaunchAngles()[0]), Toast.LENGTH_SHORT).show();
if (!target.validate()) {
Toast.makeText(SquareMethodInsertActivity.this, R.string.invalid_target, Toast.LENGTH_SHORT).show();
return;
}
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.TARGET_PARCEL, target);
bundle.putParcelableArrayList(Constants.TARGET_LIST_PARCEL, getIntent().getExtras().getParcelableArrayList(Constants.TARGET_LIST_PARCEL));
Intent targetListIntent = new Intent(this, TargetListActivity.class);
targetListIntent.putExtras(bundle);
startActivity(targetListIntent);
finish();
}
private static WeaponSettings getWeaponSettings(int distance, int temperatureIndex) throws IOException {
float tmpDistance = (float) distance / 10;
tmpDistance = round(tmpDistance);
tmpDistance *= 10;
distance = (int) tmpDistance;
int[] launchAngles = new int[3];
float[] flightTimes = new float[3];
int[] extraCharges = new int[3];
String line;
while ((line = bufferedReader.readLine()) != null) {
int distanceFromFile = 0;
String[] splittedLine = line.split(";");
String[] distanceAndCharges = splittedLine[0].split(",");
String[] launchAngleAndFlightTime = splittedLine[temperatureIndex].split(",");
String distanceString = distanceAndCharges[0].trim();
if (distanceString.matches("\\d+")) {
distanceFromFile = Integer.valueOf(distanceString);
}
if (distance != distanceFromFile) continue;
launchAngles[0] = Integer.valueOf(launchAngleAndFlightTime[0].trim());
flightTimes[0] = Integer.valueOf(launchAngleAndFlightTime[1].trim());
extraCharges[0] = Integer.valueOf(distanceAndCharges[1].trim());
break;
}
bufferedReader.close();
return new WeaponSettings(launchAngles, flightTimes, extraCharges);
}
}
logcat的:
05-16 18:44:42.346: E/AndroidRuntime(31352): FATAL EXCEPTION: main
05-16 18:44:42.346: E/AndroidRuntime(31352): java.lang.IllegalStateException: Could not execute method of the activity
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.view.View$1.onClick(View.java:3597)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.view.View.performClick(View.java:4202)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.view.View$PerformClick.run(View.java:17344)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.os.Handler.handleCallback(Handler.java:725)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.os.Looper.loop(Looper.java:137)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 18:44:42.346: E/AndroidRuntime(31352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-16 18:44:42.346: E/AndroidRuntime(31352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-16 18:44:42.346: E/AndroidRuntime(31352): at dalvik.system.NativeStart.main(Native Method)
05-16 18:44:42.346: E/AndroidRuntime(31352): Caused by: java.lang.reflect.InvocationTargetException
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.lang.reflect.Method.invoke(Method.java:511)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.view.View$1.onClick(View.java:3592)
05-16 18:44:42.346: E/AndroidRuntime(31352): ... 11 more
05-16 18:44:42.346: E/AndroidRuntime(31352): Caused by: java.lang.NullPointerException: asset
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.content.res.AssetManager.getAssetRemainingLength(Native Method)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.content.res.AssetManager.access$300(AssetManager.java:35)
05-16 18:44:42.346: E/AndroidRuntime(31352): at android.content.res.AssetManager$AssetInputStream.available(AssetManager.java:554)
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.io.InputStreamReader.read(InputStreamReader.java:234)
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
05-16 18:44:42.346: E/AndroidRuntime(31352): at java.io.BufferedReader.readLine(BufferedReader.java:354)
05-16 18:44:42.346: E/AndroidRuntime(31352): at ee.arvestus.karvastaja.SquareMethodInsertActivity.getWeaponSettings(SquareMethodInsertActivity.java:238)
05-16 18:44:42.346: E/AndroidRuntime(31352): at ee.arvestus.karvastaja.SquareMethodInsertActivity.addTarget(SquareMethodInsertActivity.java:179)
05-16 18:44:42.346: E/AndroidRuntime(31352): ... 14 more
第238行是while ((line = bufferedReader.readLine()) != null) {