2

EnterPinとConfirmPinの2つの異なるアクティビティがあるため、共有設定を使用してピンを保存しようとしています。

EnterPinの場合、[開始コード]があります

package com.example.textsmslock;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class EnterPin extends Activity {
public String[] pin = new String[4];
public Button[] buttons = new Button[10];
public int counter = 0;
public String FILENAME = "pin_file";
public static final String PinCode = "1234";

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter_pin);
    //getActionBar().setDisplayHomeAsUpEnabled(true);

    buttons[1] = (Button)findViewById(R.id.button1);
    buttons[2] = (Button)findViewById(R.id.button2);
    buttons[3] = (Button)findViewById(R.id.button3);
    buttons[4] = (Button)findViewById(R.id.button4);
    buttons[5] = (Button)findViewById(R.id.button5);
    buttons[6] = (Button)findViewById(R.id.button6);
    buttons[7] = (Button)findViewById(R.id.button7);
    buttons[8] = (Button)findViewById(R.id.button8);
    buttons[9] = (Button)findViewById(R.id.button9);
    buttons[0] = (Button)findViewById(R.id.button0);


    //Restore the preferences
    SharedPreferences settings = getSharedPreferences(PinCode, 0);


}
public void button1(View view)
{
    if(counter < 3)
    {
        pin[counter] = "1";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button2(View view)
{
    if(counter < 3)
    {
        pin[counter] = "2";
        counter++;
    }
    else
    {
        PinEntered();
    }
} 
public void button3(View view)
{
    if(counter < 3)
    {
        pin[counter] = "3";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button4(View view)
{
    if(counter < 3)
    {
        pin[counter] = "4";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button5(View view)
{
    if(counter < 3)
    {
        pin[counter] = "5";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button6(View view)
{
    if(counter < 3)
    {
        pin[counter] = "6";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button7(View view)
{
    if(counter < 3)
    {
        pin[counter] = "7";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button8(View view)
{
    if(counter < 3)
    {
        pin[counter] = "8";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button9(View view)
{
    if(counter < 3)
    {
        pin[counter] = "9";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void button0(View view)
{
    if(counter < 3)
    {
        pin[counter] = "0";
        counter++;
    }
    else
    {
        PinEntered();
    }
}
public void PinEntered()
{
if(counter == 3)
    {
        putString(pin,0);

    }
}
}

【終了コード】

ConfirmPinの場合[開始コード]があります

package com.example.textsmslock;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class ConfirmPin extends EnterPin {
public String[] pinconfirm = new String[4];
public Button[] buttons = new Button[10];
public int counter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirm_pin);
    //getActionBar().setDisplayHomeAsUpEnabled(true);
    buttons[1] = (Button)findViewById(R.id.button1);
    buttons[2] = (Button)findViewById(R.id.button2);
    buttons[3] = (Button)findViewById(R.id.button3);
    buttons[4] = (Button)findViewById(R.id.button4);
    buttons[5] = (Button)findViewById(R.id.button5);
    buttons[6] = (Button)findViewById(R.id.button6);
    buttons[7] = (Button)findViewById(R.id.button7);
    buttons[8] = (Button)findViewById(R.id.button8);
    buttons[9] = (Button)findViewById(R.id.button9);
    buttons[0] = (Button)findViewById(R.id.button0);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_confirm_pin, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
public void button1(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "1";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button2(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "2";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
} 
public void button3(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "3";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button4(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "4";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button5(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "5";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button6(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "6";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button7(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "7";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button8(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "8";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button9(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "9";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}
public void button0(View view)
{
    if(counter < 3)
    {
        pinconfirm[counter] = "0";
        counter++;
    }
    else
    {
        ConfirmingPin();
    }
}

public void ConfirmingPin()
{
          SharedPreferences settings = getSharedPreferences(PinCode, 0);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString(pinconfirm, 0);

      editor.commit();
}
}

putStringを呼び出すとエラーが発生します。誰かが理由を説明できますか?sharedPreferencesを間違って実行していますか?

4

1 に答える 1

1

putString(String key, String value)間違ったタイプで使用しています。また、追加する のと同じ値として、キーとしてではstringarrayなく、を追加しようとしています。pinconfirmの最初の項目が必要ない場合:stringputString();putString();intstring

editor.putString("PIN", pinconfirm[0].ToString());

配列からすべてを追加する場合は、単一のコンマ区切り文字列として保存するなどの操作を実行できます。

StringBuilder sb = new StringBuilder();
for (int i = 0; i < pinconfirm.length; i++) {
   sb.append(pinconfirm[i]).append(",");
}
editor.putString("pinconfirm", sb.toString());

次に、SharedPreferencesから文字列を取得したら、次のように解析します。

String[] pinconfirm= pinconfirms.split(",");
于 2012-11-26T13:54:23.600 に答える