很抱歉给你们带来了很多麻烦。
我的应用程序现已完成。现在唯一的问题是我的第一次显示欢迎屏幕是要创建新文件的位置。此欢迎屏幕的共享首选项位于文件也被读取的 main_activity 中。
结果,它甚至在创建文件之前就尝试读取文件!那么它的解决方案是什么?
主要活动代码:
package com.omm.easybalancerecharge;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
EditText num;
Button ch;
TelephonyManager operator;
String opname;
TextView status;
TextView setID;
String myID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
setstatus();
setIDNO();
//Checks and displays Welcome Screen
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref,
false);
if (!welcomeScreenShown) {
Intent welcome = new Intent("android.intent.action.WELCOME");
startActivity(welcome);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit();
}
//Reading Data from the file to be created at welcome screen for the first time.
myID = readData();
recharge();
}
//readData method
protected String readData() {
// TODO Auto-generated method stub
String ret = "";
try {
FileInputStream fIN = openFileInput("iqid");
InputStreamReader in = new InputStreamReader(fIN);
BufferedReader br = new BufferedReader(in);
ret = br.readLine();
} catch (FileNotFoundException e) {
Log.e("ID ACTIVITY", "File Not Found");
} catch (IOException e) {
Log.e("ID ACTIVITY", "Cannot Read From File");
}
return ret;
}
}
第一次欢迎屏幕的代码:
package com.omm.easybalancerecharge;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FirstTime extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_time);
final EditText ID = (EditText) findViewById(R.id.IDNO);
Button save = (Button) findViewById(R.id.sButton);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String myFile = ID.getText().toString();
FileOutputStream fOS = openFileOutput("iqid", Context.MODE_PRIVATE);
fOS.write(myFile.getBytes());
Toast.makeText(getBaseContext(), "ID Saved",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("Exception", "Save Failed");
} finally {
finish();
}
}
});
}
}
这个问题是唯一阻止我的应用程序在没有任何 FC 的情况下成功运行的问题。当然,所有权限都在清单中设置。
编辑:显然该文件根本没有被创建:/