0

很抱歉给你们带来了很多麻烦。

我的应用程序现已完成。现在唯一的问题是我的第一次显示欢迎屏幕是要创建新文件的位置。此欢迎屏幕的共享首选项位于文件也被读取的 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 的情况下成功运行的问题。当然,所有权限都在清单中设置。

编辑:显然该文件根本没有被创建:/

4

1 回答 1

1

您应该使用 startActivityForResult:

Intent i = new Intent(this, FirstTime.class);
startActivityForResult(i, 1);

在您的 FirstTime 中设置要返回到 MainActivity 的数据,如果您不想返回,请不要设置任何数据。

例如:在 FirstTime 中,如果您想发回数据

 Intent returnIntent = new Intent();
 returnIntent.putExtra("result",result);
 setResult(RESULT_OK,returnIntent);     
 finish();

如果您不想返回数据

Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);        
finish();

现在在您的 MainActivity 类中为 onActivityResult() 方法编写以下代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {  
    //Reading Data from the file to be created at welcome screen for the first time.
    myID = readData();
    recharge();

这应该可以解决问题。

于 2013-01-17T22:51:10.547 回答