初学android 想要实现应用程序个人信息的存储,在xml中找到了控件但却无法实现setText

qq_34242806 2017-02-11 08:18:38

android初学者一枚。。。学做类似于淘宝之类的手机APP,勉勉强强做出来了底部菜单栏,用ViewPage来做,每一个page对应一个XML,当手指在ViewPage左右滑动时,就相应显示不同的page。

我在个人信息的page里设置了按钮,点击进入名为“ChangePersonal”的activity,希望的是能实现修改个人资料的功能
我是用SharedPreference方法来存储个人信息,想在ChangePersonal这个activity中获取EditText的字符串后直接在MainActivity里修改。

现在遇到的问题是在ChangePersonal里数据存也存进去了,在MainActivity里也能够找到相应需要更改信息的控件,唯独差在setText这一步上,调试显示确实更改了相应TextView控件的text,但手机上就是没显示出来,实在不懂为什么,希望大家帮忙看看。。。。。。

MainActivity中一部分代码:
public class MainActivity extends ActionBarActivity implements OnClickListener,OnPageChangeListener{
// 底部菜单4个Linearlayout
private LinearLayout ll_home;
private LinearLayout ll_address;
private LinearLayout ll_friend;
// 底部菜单4个ImageView
private ImageView iv_home;
private ImageView iv_address;
private ImageView iv_friend;
// 底部菜单4个菜单标题
private TextView tv_home;
private TextView tv_address;
private TextView tv_friend;

// 中间内容区域
private ViewPager viewPager;

// ViewPager适配器ContentAdapter
private ContentAdapter adapter;

private List<View> views;

//个人信息界面
private TextView name,sex,phone,address,mail,addresss;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//acitity_mian的xml中只include了底部菜单栏
setContentView(R.layout.activity_main);
// 初始化控件
initView();
// 初始化底部按钮事件
initEvent();
getSupportActionBar().hide();
}
//这里是跳转至修改个人信息的按钮监听事件
public void click_personal(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, ChangePersonal.class);
startActivityForResult(intent, 1000);
}
//在ChangePersonal点击相应Button后回到这个函数,问题就出在这个函数中的setText,调试显示修改过了Text,手机上却不显示
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1000 && resultCode == 1001) {
SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
Activity.MODE_PRIVATE);
String Sname = sharedPreferences.getString("name", "");
name.setText(Sname);
}
}
private void initEvent() {
// 设置按钮监听
ll_home.setOnClickListener(this);
ll_address.setOnClickListener(this);
ll_friend.setOnClickListener(this);
//设置ViewPager滑动监听
viewPager.setOnPageChangeListener(this);
}
private void initView() {
// 底部菜单4个Linearlayout
this.ll_home = (LinearLayout) findViewById(R.id.ll_home);
this.ll_address = (LinearLayout) findViewById(R.id.ll_address);
this.ll_friend = (LinearLayout) findViewById(R.id.ll_friend);

// 底部菜单4个ImageView
this.iv_home = (ImageView) findViewById(R.id.iv_home);
this.iv_address = (ImageView) findViewById(R.id.iv_address);
this.iv_friend = (ImageView) findViewById(R.id.iv_friend);
// 底部菜单4个菜单标题
this.tv_home = (TextView) findViewById(R.id.tv_home);
this.tv_address = (TextView) findViewById(R.id.tv_address);
this.tv_friend = (TextView) findViewById(R.id.tv_friend);

// 中间内容区域ViewPager
this.viewPager = (ViewPager) findViewById(R.id.vp_content);
// 适配器
View page_01 = View.inflate(MainActivity.this, R.layout.home, null);
View page_02 = View.inflate(MainActivity.this, R.layout.missionlist, null);
View page_03 = View.inflate(MainActivity.this, R.layout.personal, null);

views = new ArrayList<View>();
views.add(page_01);
views.add(page_02);
views.add(page_03);

this.adapter = new ContentAdapter(views);
viewPager.setAdapter(adapter);
//个人信息控件
name = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_name);
phone = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_phone);
address = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_address);
mail = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_mail);
sex = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_sex);
}
@Override
public void onClick(View v) {
// 在每次点击后将所有的底部按钮(ImageView,TextView)颜色改为灰色,然后根据点击着色
restartBotton();
// ImageView和TetxView置为绿色,页面随之跳转
switch (v.getId()) {
case R.id.ll_home:
iv_home.setImageResource(R.drawable.guide_home_on);
tv_home.setTextColor(0xff1B940A);
viewPager.setCurrentItem(0);
break;
case R.id.ll_address:
iv_address.setImageResource(R.drawable.guide_cart_on);
tv_address.setTextColor(0xff1B940A);
viewPager.setCurrentItem(1);
break;
case R.id.ll_friend:
iv_friend.setImageResource(R.drawable.guide_account_on);
tv_friend.setTextColor(0xff1B940A);
viewPager.setCurrentItem(2);
break;
default:
break;
}

}
...全文
315 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
violet3890 2017-12-18
  • 打赏
  • 举报
回复
请问是什么问题啊 我也遇到了 朋友
satanmeng 2017-11-06
  • 打赏
  • 举报
回复
怎么解决的?
我也是相同的问题。
qq_34242806 2017-03-06
  • 打赏
  • 举报
回复
我解决了……谢谢各位帮忙,只是在findviewbyid前加上他所对应的页面…
jscoolstar 2017-02-14
  • 打赏
  • 举报
回复
name = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_name); phone = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_phone); address = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_address); mail = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_mail); sex = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_sex) 你这个写法等价于: View v1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null); name = v1.findViewById(R.id.tv_name); View v2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null); phone = v2.findViewById(R.id.tv_phone ); .... 然后你肯定有个地方这样用的 view v3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null); return v3; 或者是setcontentview(R.layout.personal). 你相当于显示了v3,却在用v1的name.settext
jscoolstar 2017-02-14
  • 打赏
  • 举报
回复
谁教你这样写的: name = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_name); phone = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_phone); address = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_address); mail = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_mail); sex = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_sex); 我保证不打死他。LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null),这个东西会返回一个view,你多次调用,其实返回了多个view,你是在多胞胎里面一个人上面找眼睛,一个上面找鼻子,再另一个上面找耳朵。然后其中一个是你用于显示的,你却让另一个人的眼睛看东西,却怪你当前显示的这个人看不到东西?
头发还没秃a 2017-02-13
  • 打赏
  • 举报
回复
引用 2 楼 ink_s 的回复:
是不是你 name.setText(Sname); 在 name = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_name); 之前调用了?
你那样说的话就不是不显示而是直接报错了
ink_s 2017-02-13
  • 打赏
  • 举报
回复
是不是你 name.setText(Sname); 在 name = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.personal, null).findViewById(R.id.tv_name); 之前调用了?
  • 打赏
  • 举报
回复
不懂就顶贴。
YXTS122 2017-02-13
  • 打赏
  • 举报
回复
不会,帮你顶顶。。。。。。。。。。。
ganshenml 2017-02-13
  • 打赏
  • 举报
回复
Sname的值输出下,看有没有值。另外,这个要用驼峰写法规则一点:sName

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧