编辑开始
很抱歉弄乱了我的代码。你们都指出了各种错误,你们都是正确的。但真正发生的是我的原始代码在一定程度上是正确的。在这里发布代码之前,我更改了我的原始代码以检查某些内容,我在这里使用了编辑过的代码,忘记了我改变了,这就是你看到一些错误的文本视图、大写等的原因。为此道歉。
我检查了我的代码,用按钮所在的同一活动上的文本视图替换了位于其他活动上的文本视图,并且工作正常。所以我猜我的 Intent to start another activity 导致了错误。这是我第一次尝试使用 Intent。我用的是
Intent intent=new Intent(this, ViewAccount.class);
startActivity(intent);
但这似乎行不通。简而言之,我有三个活动 activity1 包含 editText1 editText2 和一个保存按钮。当保存按钮单击来自使用 sharedPreferences 存储的 edtiText1 和 editText2 的数据时。activity2 包含一个名为 show 的按钮。单击按钮显示时,应打开活动3,并在其上保存值。
我希望我没有让它变得更复杂。
编辑结束。
旧帖
有人可以帮我找出我做错了什么。我只是 Java 和 Android 编程世界的新手。我只是想制作一个示例应用程序,我将在其中使用 SharedPreferences 保存和检索数据。保存部分工作正常,我还为保存举杯。但是当我单击按钮以检索数据时,应用程序崩溃了。请帮帮我。在保存时,我使用 Name 和 Age 作为键。
以下是我用于检索数据的 java 代码:
package com.xyz.abc;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class Card extends Activity{
public static final String DEFAULT="N/A";
TextView showsname,showage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card);
showname=(TextView)findViewById(R.id.showname);
showage=(TextView) findViewById(R.id.showage);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.card, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void ViewAC(View view) {
SharedPreferences sharedPreferences=getSharedPreferences("data", Context.MODE_PRIVATE);
String SName=sharedPreferences.getString("Name",DEFAULT);
String sAge=sharedPreferences.getString("Age",DEFAULT);
if(SName.equals(DEFAULT)|| sAge.equals(DEFAULT))
{
Toast.makeText(this,"There is no account exist",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this,"Account loaded successfully",Toast.LENGTH_LONG).show();
showsitename.setText(sName);
showurl.setText(sAge);
}
Intent intent=new Intent(this, ViewAccount.class);
startActivity(intent);
}
}
以下是xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.xyz.abc.ViewAccount"
android:orientation="vertical">
<TextView
android:text="Website Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showname"/>
<TextView
android:text="URL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/showage"/>
</LinearLayout>