Java 为什么我的应用程序';不幸的是,abc已经停止了';

Java 为什么我的应用程序';不幸的是,abc已经停止了';,java,android,android-xml,android-sharedpreferences,Java,Android,Android Xml,Android Sharedpreferences,编辑开始 对不起,把我的密码弄乱了。你们都指出了各种错误,你们都是正确的。但真正发生的是我的原始代码在一定程度上是正确的。在这里发布代码之前,我更改了我的原始代码以检查某些内容,并且我使用了这里编辑的代码,但忘记了我已更改,这就是您看到一些错误文本视图、大写等的原因。对此表示歉意 我检查了我的代码,将另一个活动中的文本视图替换为按钮所在的同一活动中的文本视图,它工作正常。所以我想是我想开始另一项活动的意图导致了这个错误。这是我第一次尝试使用Intent。我用的都是 Intent intent=

编辑开始

对不起,把我的密码弄乱了。你们都指出了各种错误,你们都是正确的。但真正发生的是我的原始代码在一定程度上是正确的。在这里发布代码之前,我更改了我的原始代码以检查某些内容,并且我使用了这里编辑的代码,但忘记了我已更改,这就是您看到一些错误文本视图、大写等的原因。对此表示歉意

我检查了我的代码,将另一个活动中的文本视图替换为按钮所在的同一活动中的文本视图,它工作正常。所以我想是我想开始另一项活动的意图导致了这个错误。这是我第一次尝试使用Intent。我用的都是

 Intent intent=new Intent(this, ViewAccount.class);
            startActivity(intent);
但这似乎不起作用。 简而言之,我有三个活动activity1包含editText1 editText2和一个保存按钮。当“保存”按钮单击使用SharedReferences存储的edtiText1和editText2中的数据时。activity2包含一个名为show的按钮。单击“显示”按钮时,应打开“活动3”,并在其上保存值

我希望我没有把事情弄得更复杂

编辑结束


旧职位

有人能帮我找出我做错了什么吗。我只是Java和Android编程世界的新手。我只是想制作一个示例应用程序,我将使用SharedReferences保存和检索数据。储蓄部分很有效,我也为储蓄干杯。但当我点击按钮检索数据时,应用程序崩溃了。请帮帮我。 保存时,我使用姓名和年龄作为键

以下是我用于检索数据的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>

首先,您应该包括日志猫,因为它通常会给出错误的确切原因

其次,我在XML中看不到按钮,也看不到“onClick”。您单击什么来执行代码

现在是代码

这可能是造成这次事故的原因:

showsitename.setText(sName);
showurl.setText(sAge);

您从未定义名为“showsitename”和“showurl”的文本视图。您确实有名为“showsname”和“showage”的文本视图。您打算使用这些吗?

showsitename.setText(sName);showurl.setText(sAge);如果您使用了错误的文本视图,则此处的文本视图为showname和showAge从日志中发布错误代码引发了异常。查看相关原因的跟踪日志。读取结果正确。文本视图是错误的。另外,您的代码还有三个错误。最主要的一点是,设置SName时不应将其大写。而且,这是错误的:文本视图showsname,showage;(您在showname中添加了一个s)。这并不能真正回答问题。请耐心等待,在任何时候你都不会有足够的代表发表评论。是的,如果你没有答案,请不要将其留在答案框中