編集開始
私のコードを台無しにして申し訳ありません。いろいろな間違いを指摘してくださった皆さん、正解です。しかし、実際に起こったことは、私の元のコードがある程度まで正しかったということです。ここにコードを投稿する前に、元のコードを変更して何かを確認し、変更したことを忘れて編集したコードをここで使用しました。
他のアクティビティにあるテキストビューを、ボタンがある同じアクティビティのテキストビューに置き換えるコードを確認しましたが、正常に機能しました。したがって、別のアクティビティを開始する意図がエラーを引き起こしたと思います。初めてIntentを使ってみました。私が使ったのは
Intent intent=new Intent(this, ViewAccount.class);
startActivity(intent);
しかし、それはうまくいかないようです。簡単に言うと、editText1 editText2 と保存ボタンを含む 3 つのアクティビティ activity1 があります。保存ボタンをクリックすると、sharedPreferences を使用して保存された edtiText1 および editText2 からのデータ。activity2 には、show というボタンが含まれています。ボタン表示がクリックされると、値が保存された状態で activity3 が開かれます。
これ以上複雑にしなかったことを願っています。
編集終了。
古い投稿
誰かが私が間違っていることを見つけるのを手伝ってくれますか? 私は Java および Android プログラミングの世界の初心者です。SharedPreferences を使用してデータを保存および取得するサンプル アプリを作成しようとしています。保存部分は正常に機能しています。保存時にトーストも入れました。しかし、ボタンをクリックしてデータを取得すると、アプリがクラッシュします。私を助けてください。保存中、名前と年齢をキーとして使用しました。
以下は、データの取得に使用した 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>