好的,感谢以下伙计最大的问题已解决。但是无论我打印什么,都不会打印任何内容,也无法打印出在第2类(nimekysija)中键入的消息:(。我真的需要它存储名称,以后它将每次写下名称!谢谢!你的帮助!

问题必须在二等。当我更新editor.putString(“ nimi2”,nimiS); nimiS变成“ plapla”,则plapla实际上显示为:/。所以我真的不知道,这是什么问题!

(以下类也已更新为最新)



第1类:

package viimane.voimalus;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;

public class MainStuff extends Activity {
    TextView tere;
    String nimi;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);
        tere = (TextView) findViewById(R.id.tvTere);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
        if (nimiOlemas == false){
             startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
             finish();
        }
        if (nimiOlemas == true){
            nimi = preferences.getString("nimi2", "");
            System.out.print("töötab!");

            tere.setText("Tere " + nimi);
        }
        System.out.print("töötab2!");
    }
}




2类

package viimane.voimalus;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class nimekysija extends Activity {

    EditText nimi;
    SharedPreferences preferences;
    String nimiS;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nimekysija);
        preferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        nimi = (EditText) findViewById(R.id.etNimekysija);
        nimiS = nimi.getText().toString();
        Button kysOk = (Button) findViewById(R.id.bNimekysija);
        kysOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("nimi2", nimiS); // nime kirjutamine
                editor.putBoolean("nimionolemas", true); // nimi on kirjutatud!
                editor.commit();
                startActivity(new Intent("viimane.voimalus.MAINSTUFF"));
                finish();

            }
        });
    }

}

最佳答案

好吧,我猜您可能是Java新手,如果我不正确,请原谅我。您永远不会从nimiOlemas中阅读。

 boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
    nimiOlemas = false;
                startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
                finish();
               nimiOlemas = true;


我认为您要尝试执行的操作是初始化nimiOlemas,然后将其初始化(如果为假),启动一个活动,调用finish,然后将nimiOlemas设置为true,但这不是您在做什么。这是你想要的吗?

boolean nimiOlemas = preferences.getBoolean("nimionolemas", false);
if (nimiOlemas == false)
{
     startActivity(new Intent("viimane.voimalus.NIMEKYSIJA"));
     finish();
     nimiOlemas = true;
}


=是赋值,==是布尔比较。您在问题中说要检查布尔值,但从不这样做,您只分配给它。

10-08 18:34