【问题标题】:Why my app 'Unfortunately, abc has stopped'为什么我的应用程序“不幸的是,abc 已停止”
【发布时间】:2014-07-26 21:21:47
【问题描述】:

编辑开始

抱歉弄乱了我的代码。你们都指出了各种错误,你们都是正确的。但真正发生的是我的原始代码在一定程度上是正确的。在这里发布代码之前,我更改了我的原始代码以检查某些内容,我在这里使用了编辑过的代码,忘记了我改变了,这就是你看到一些错误的文本视图、大写等的原因。为此道歉。

我检查了我的代码,将位于其他活动上的文本视图替换为按钮所在的同一活动上的文本视图,并且工作正常。所以我猜我的 Intent to start another activity 导致了错误。这是我第一次尝试使用 Intent。我只用了

 Intent intent=new Intent(this, ViewAccount.class);
            startActivity(intent);

但这似乎不起作用。 简而言之,我有三个活动 activity1 包含 editText1 editText2 和一个保存按钮。当保存按钮单击来自使用 sharedPreferences 存储的 edtiText1 和 editText2 的数据时。 activity2 包含一个名为 show 的按钮。当按钮显示被点击时,activity3 应该打开并保存值。

我希望我没有让它变得更复杂。

编辑结束。


旧帖

谁能帮我找出我做错了什么。我只是 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>

【问题讨论】:

  • showsitename.setText(sName); showurl.setText(sAge);我猜你在这里使用了错误的 textview 你的 textviwes 是 showname 和 showage
  • 从 LogCat 发布您的错误
  • 代码抛出异常。查看相关原因的跟踪日志。
  • kgandroid 是正确的。 TextViews 是错误的。另外,您的代码还有其他三个错误。主要的是 SName 在设置时不应大写。此外,这是错误的:TextView 显示名称,显示; (你在 showname 中添加了一个 s)。

标签: java android android-xml android-sharedpreferences


【解决方案1】:

首先,您应该包含您的 LogCat,因为它通常会为您提供错误的确切原因。

其次,我在您的 XML 中没有看到按钮,也没有看到“onClick”。你点击什么来执行代码?

现在是代码。

这可能是崩溃的原因:

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

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

【讨论】:

  • 这并不能真正回答问题。请耐心等待,您很快就会有足够的代表发表评论。
  • 是的,如果您没有答案,请不要将其留在答案框中
猜你喜欢
  • 2016-10-03
  • 2018-03-23
  • 2015-05-09
  • 2016-11-19
  • 1970-01-01
  • 2016-11-27
  • 2014-10-20
相关资源
最近更新 更多