Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在一个活动中保存我的值,并在不同的活动中显示此值_Android - Fatal编程技术网

Android 在一个活动中保存我的值,并在不同的活动中显示此值

Android 在一个活动中保存我的值,并在不同的活动中显示此值,android,Android,我尝试将密码保存到一个活动中&我想将其恢复到不同的活动中,但当我的应用程序启动第二个活动时,它会崩溃。 有人能帮我吗 package com.example.test; public class MainActivity extends Activity { String finall; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(save

我尝试将密码保存到一个活动中&我想将其恢复到不同的活动中,但当我的应用程序启动第二个活动时,它会崩溃。 有人能帮我吗

package com.example.test;

public class MainActivity extends Activity {

    String finall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String FILENAME = "hello_file.txt";
        String string = "1234";

        FileOutputStream fos;
        try
        {
            fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            fos.write(string.getBytes());
            fos.close();
        } 
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }

        try
        {
            FileInputStream in = openFileInput("hello_file.txt");
            StringBuffer fileContent = new StringBuffer("");
            byte[] buffer = new byte[4];

            while(in.read(buffer) != -1)
            {
                fileContent.append(new String(buffer));
            }
            finall = fileContent.toString();
            in.close();
        }
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }

        Button button = (Button)findViewById(R.id.button);
        button.setText(finall);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                sendGo(v);

            }
        });
    }

    public void sendGo(View v)
    {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
}
第一部分工作,因为我可以在同一活动中读取保存的文件。 但是当我试图把它读入另一个活动时,它不起作用:

package com.example.test;

public class SecondActivity extends Activity {

    String finall="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        // Show the Up button in the action bar.
        setupActionBar();

        try
        {
            FileInputStream in = openFileInput("hello_file.txt");
            StringBuffer fileContent = new StringBuffer("");

            byte[] buffer = new byte[4];

            while(in.read(buffer) != -1)
            {
                fileContent.append(new String(buffer));
            }
            finall = fileContent.toString();
            in.close();
        }
        catch (FileNotFoundException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); }

        TextView text = (TextView)findViewById(R.id.mehmet);
        text.setText(finall);
    }



    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

我所知道的在活动之间传递变量的最简单方法如下:

在您的活动中,您创建了一个新的意图:

Intent intent = new Intent(getApplicationContext(), NextActivity.class);
intent.putExtra("nameofvariable","valueofvariable");
startActivity(intent);
从下一个活动中,您可以通过以下方式检索此值:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("nameofvariable");
}
但是,使用FileInputStream的正确方法是:

FileInputStream in = openFileInput("hello_file.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }

我在emulator(8级)和三星galaxy S3(16级)上测试了你的应用程序,效果很好

第二个活动的文本视图是1234

您在哪些设备上进行测试

你能寄给我们你的日志吗

不要错过在AndroiManifest.xml文件中声明所有活动


谢谢

如果您想在活动之间共享数据,可以使用:

  • 意图并将额外数据放入捆绑包中
  • 应用程序类中的SharePreferences
  • SQLite数据库
  • 静态变量

如果希望在活动之间共享数据,可以执行以下操作:

  • 意图并将额外数据放入捆绑包中
  • SharePreferences(即使应用程序关闭并重新启动,您也可以访问它)
  • SqLite数据库(即使应用程序关闭并重新启动,您也可以访问它)
  • 静态类
  • 这就是如何使用SharedPrefs->

    SharedPreferences pref = getBaseContext().getSharedPreferences(PREFERENCE_NAME, 0); 
    SharedPreferences.Editor editor editor = pref.edit();
    editor.putString("KEY_PASSORD", "Password to Save");
    editor.commit();
    
    当您想从SharedPref检索密码时,请执行以下操作->

    SharedPreferences Pref = getBaseContext().getSharedPreferences(PREFERENCE_NAME, 0);
    String password = pref.getString("KEY_PASSWORD","Default Password");
    

    您可以在first activty中使用SharePreferences并将密码保存到其中:

    package com.example.test;
    
    public class FirstActivity extends Activity {
    SharedPreferences pref;
    SharedPreferences.Editor editor editor;
    
    private final String KEY_PASSWORD = "password";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        Button button = (Button)findViewById(R.id.button);
        button.setText(finall);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                pref  = getSharedPreferences("passwordPref", 0); 
                SharedPreferences.Editor editor editor = pref.edit();
                editor.putString(KEY_PASSWORD ,YourEditBox.getText());
                editor.commit();
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivity(intent);
    
            }
        });
    
    }
    
    }
    
    在第二个活动中,请使用此选项检索密码:

    package com.example.test;
    
    public class SecondActivity extends Activity {
    SharedPreferences pref;
    SharedPreferences.Editor editor editor;
    
    private final String KEY_PASSWORD = "password";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        pref = getSharedPreferences("passwordPref", 0);
        YourEditText.setText(pref.getString(KEY_PASSWORD,""));
    }
    
    }
    

    发布您的日志数据我认为您应该使用startActivityForResult,而不是startActivity。。无论如何,发布您的日志猫,它将帮助我们查看导致应用程序崩溃的原因确保您在
    AndroidManifest.xml
    中声明了
    SecondActivity
    Activity?@StinePike如何共享我的日志猫?我给你发了一张它的图片(小屏幕)@ρ∑ρK是的,我已经做了!是的,但是当你使用putExtra作为目的时,这个变量不会被保存。在这里,我尝试保存密码并将其显示为另一个活动。我尝试使用SharedReferences,但它不太管用。。我会尝试你的第二种解决方案,我回来;)@原谅你的错误。编辑您的问题,并将其附在底部。这不仅是为了在活动之间共享数据,同时还可以保存这些数据。因为在这里,我尝试保存一个密码…好的,谢谢,但我展示的只是一个测试,因为我的目标是创建一个登录页面,用户只输入一个密码(而不是用户名)。在这个主要活动中,我添加了一个按钮来更改他的密码。因此,我想在新活动中恢复此新密码并更改MainActivity中的旧密码..是的,这应该可以…如果您在新活动中恢复/重置密码并希望在MainActivity中获得相同的密码,则两者都将与应用程序上下文中维护的prefs相同Yeah,你能帮我一些想法吗,但我以前试过,但不管用。在那之后,我尝试了文件等。如果你对我有任何想法,你会觉得很酷:)好的……我会在github上发布相关代码,并在明天给你URL。我的朋友,我会尝试你的代码,但你能在我以前的代码中编辑你的答案吗?非常感谢你