對不起,我的代碼搞亂了。你們所有人都指出了各種錯誤,你們都是對的。但真正發生的是我的原始代碼在某種程度上是正確的。在此處發佈代碼之前,我更改了原始代碼以檢查某些內容,並在此處使用了編輯後的代碼,忘記了我更改了,這就是您看到一些錯誤文本視圖,大寫等原因。請道歉。
我檢查了我的代碼替換位於其他活動的文本視圖與文本視圖上的按鈕是相同的活動,它工作正常。所以我想我的意圖開始另一個活動造成的錯誤。這是我第一次嘗試使用Intent。我所用的全部是
Intent intent=new Intent(this, ViewAccount.class);
startActivity(intent);
但似乎不起作用。爲了簡化它,我有三個活動activity1包含editText1 editText2和一個保存按鈕。保存按鈕單擊edtiText1和editText2中使用sharedPreferences存儲的數據時。 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>
showsitename.setText(sName); showurl.setText(sAge);我想你在這裏使用的是錯誤的textview你的textviwes顯示名稱和顯示 – kgandroid
從LogCat發佈你的錯誤 – arleitiss
代碼拋出一個異常。查看跟蹤日誌以查找相關原因。 – user2864740