求大神帮我看看我的代码哪里出错了
androidstudio吧
全部回复
仅看楼主
level 1
想用SharedPrefernces实现数据存储,但却显示不出来求大神花点时间帮我看看哪里错了
2017年04月22日 06点04分 1
level 1
MainActivity的代码:
package com.example.administrator.app0502;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText username;
private EditText password;
private Button login;
String strName;
String strPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username=(EditText)findViewById(R.id.et_username);
password=(EditText)findViewById(R.id.et_psaaword);
login=(Button)findViewById(R.id.but_login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
strName=username.getText().toString();
strPassword=password.getText().toString();
FileOutputStream fos=null;
try{
fos=openFileOutput("LOGIN",MODE_PRIVATE);
fos.write((strName+""+strPassword).getBytes());
fos.flush();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if (fos!=null){
try{
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
Intent intent=new Intent();
intent.setClass(MainActivity.this,ReadActivity.class);
startActivity(intent);
}
});
}
}
2017年04月22日 06点04分 2
level 1
ReadActivity的代码:
package com.example.administrator.app0502;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by Administrator on 2017/4/10.
*/
public class ReadActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
setLoginInfo();
}
private void setLoginInfo(){
TextView username,password;
String data,strUsername,strPassword;
FileInputStream fis=null;
byte[]buffer=null;
try{
fis=openFileInput("LOGIN");
buffer=new byte[fis.available()];
fis.read(buffer);
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally {
if(fis!=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
username=(TextView)findViewById(R.id.tvUsername2);
password=(TextView)findViewById(R.id.tvPassword2);
data=new String(buffer);
strUsername=data.split("")[0];
strPassword=data.split("")[1];
username.setText("用户名:"+strUsername);
password.setText("密 码:"+strPassword);
}
}
2017年04月22日 06点04分 3
level 1
不知道哪里错了,用户名和密码在第二个布局文件里显示不出来
2017年04月22日 06点04分 4
level 9
我很想知道sharedpreferences和你发的代码有什么关系
2017年04月22日 11点04分 5
level 8
半毛钱关系,取不出,1.你存进去了?2.存进去了,是不是健名不一致
2017年04月23日 09点04分 7
1