简体   繁体   English

Android AVD给出“不幸的是[App]已停止工作”错误

[英]Android AVD is giving an “Unfortunately [App] Has Stopped Working” error

I have just started learning to develop Android apps. 我刚刚开始学习开发Android应用程序。 In this project, I have used a Menu whose second option leads to a class called TextPlay. 在该项目中,我使用了Menu,其第二个选项导致了一个名为TextPlay的类。 This works just fine till the time I add the line, 在我添加行之前,这一直很好

display.setText(input.getText().toString());

in TextPlay.java. 在TextPlay.java中。

TextPlay.java TextPlay.java

package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity{ 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.text);
        Button chkCmd = (Button)findViewById(R.id.bResults);
        ToggleButton passTog = (ToggleButton)findViewById(R.id.tbPassword);
        EditText input = (EditText)findViewById(R.id.etCommands);
        TextView display = (TextView)findViewById(R.id.tvDisplay);

        display.setText(input.getText().toString());

    }
}

Text.xml Text.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="25sp" >

    <EditText
        android:id="@+id/etCommands"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Type a Command"
        android:password="true" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

        <Button
            android:id="@+id/bResults"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:text="Button" />

        <ToggleButton
            android:id="@+id/tbPassword"

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:checked="true"
            android:text="ToggleButton" />
    </LinearLayout>

    <TextView
        android:id="@+id/tvResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="TextView" />

</LinearLayout>
TextView display = (TextView) findViewById(R.id.tvDisplay);

您的TextView似乎被称为R.id.tvResults但是在findViewById() R.id.tvDisplay其命名为R.id.tvDisplay ,因此它不存在并返回null ,这就是为什么看到此异常的原因。

您的布局中没有ID为“ tvDisplay”的TextView

In Text.xml, you have saved it with different name: android:id="@+id/tvResults" 在Text.xml中,已使用其他名称保存了它: android:id =“ @ + id / tvResults”

correct it with android:id="@+id/tvDisplay android:id =“ @ + id / tvDisplay纠正它

you are getting this error because their is no tvDisplay id in R.java file. 您收到此错误,因为在R.java文件中它们不是tvDisplay id。 that is why it is sending null exception. 这就是为什么它发送空异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM