为什么'不幸的是我的应用程序已停止'在Android模拟器中出现 [英] Why 'unfortunately my application has stopped ' in android emulator is appearing

查看:172
本文介绍了为什么'不幸的是我的应用程序已停止'在Android模拟器中出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正常工作,并出现在模拟器的屏幕上。我为num1和num2得到2个数字并使用'添加'按钮并在'回答'中显示答案。我可以在num1中输入一个数字但是无法输入num2,因为我无法将光标放在num2上。



如果我点击添加按钮它抛出一个错误,说不幸的是app停止工作。在设计模式中,我检查了'clickable'属性,在'onClick'中我也选择了'onButtonClick'。



  public   class  MainActivity  extends 活动{

@覆盖
protected void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
public void onButtonClick(查看v)
{
int n1,n2,ans = 0;
EditText e1 =(EditText)findViewById(R.id.num1);
EditText e2 =(EditText)findViewById(R.id.num2);
TextView view_answer =(TextView)findViewById(R.id.answer);
n1 = Integer.parseInt(e1.getText()。toString());
n2 = Integer.parseInt(e2.getText()。toString());
ans = n1 + n2;
view_answer.setText(Integer.toString(ans));
}


@覆盖
public boolean onCreateOptionsMenu(菜单菜单){
// 膨胀菜单;如果项目存在,则会将项目添加到操作栏。
getMenuInflater()。inflate(R.menu.menu_main,menu);
返回 true;
}


}





< ; relativelayout xmlns:android =http://schemas.android.com/apk/res/android> 
xmlns:tools =http://schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent>

< Button
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Add
android: id =@ + id / add
android:layout_centerVertical =true
android:layout_centerHorizo​​ntal =true
android:clickable =true
android:nestedScrollingEnabled = 假
android:/>

< textview>
android:layout_width =wrap_content
android:layout_height =wrap_content
android:textAppearance =?android:attr / textAppearanceLarge

android:id = @ + id / answer
android:layout_alignParentBottom =true
android:layout_centerHorizo​​ntal =true
android:layout_marginBottom =90dp
android:width =123dp
android:removed =#ffcbffff/>

< edittext>
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / num1
android:layout_alignParentTop =true
android:layout_alignRight =@ + id / add
android:layout_alignEnd =@ + id / add
android:layout_marginTop =45dp
android:width =65dp
android:removed =#fffeffa0/>

< edittext>
android:layout_width =wrap_content
android:layout_height =wrap_content
android:id =@ + id / num2
android:width =65dp
android:removed =#ffffa6f0
android:layout_below =@ + id / num1
android:layout_centerHorizo​​ntal =true
android:textSize =20dp
android:textIsSelectable =true
android:height =45dp
android:layout_marginTop =88dp/>
< / edittext>< / edittext>< / textview>< / relativelayout>


< resources>

< string name =app_name>我的应用程序< / string>
< string name =hello_world> Hello world!< / string>
< string name =action_settings>设置< / string>
< string name =title_activity_main> MainActivity< / string>

< string name =num1>< / string>
< string name =num2>< / string>
< string name =answer>< / string>
< string name =add>添加< / string>

< / resources>

解决方案

不幸,activity_main.xml一团糟,除其他外,还有不正确形成的标签,无效的属性和错误的字母大小写。更改为:



 <   RelativeLayout     xmlns:android   =  http://schemas.android.com/apk/res/android  

xmlns:tools = http://schemas.android.com/tools

android:lay out_width = match_parent

android:layout_height = match_parent

tools:context = 。MainActivity >
< 按钮

android:id = @ + id / add

android:layout_width = wrap_content

android:layout_height = wrap_content

android:layout_centerHorizo​​ntal = true

android:layout_centerVertical = true

android:/ >
< TextView

android:id = @ + id / answer

android:layout_width = wrap_content

android:layout_height = wrap_content

android:layout_alignParentBottom = < span class =code-keyword> true

< span class =code-attribute> android:layout_centerHorizo​​ntal = true

android:layout_marginBottom = 90dp

android:width < span class =code-keyword> = 123dp
< span class =code-attribute>
android:textAppearance = ?android:attr / textAppearanceLarge / >
< EditText

android:id = @ + id / num1

< span class =code-attribute> android:layout_width = wrap_content

android: layout_height = wrap_content

< span class =code-attribute> android:layout_alignEnd = @ + id / add

android:layout_alignParentTop = true

android:layout_alignRight = @ + id / add

android:layo ut_marginTop = 45dp

android:width = 65dp / >
< < span class =code-leadattribute> EditText

< span class =code-attribute> android:id = @ + id / num2

android:layout_width = wrap_content

android:layout_height = wrap_content

android:layout_below = @ + id / num1

android:layout_centerHorizo​​ntal = true

android:layout_marginTop = 88dp

android:width = 65dp

android:height = 45dp

android:textSize = 20dp / >
< / RelativeLayout >



无法编辑num2edittext的原因是包含

 android:textIsSelectable =true 



参考: Android UI布局和控件

这应该可以解决您的问题。

强烈建议您通过 http从这12篇获奖文章中重新学习Android开发://www.codeproject.com/Competitions/757/Android-Tutorial-Contest.aspx


My App works and appears on the screen in the emulator. I get 2 numbers for num1 and num2 and use the 'Add' button and show the answer in 'answer'. I am able to enter a number in num1 but unable to enter for num2 as I could not place the cursor on num2.

If I click the 'Add' button it throws an error stating 'unfortunately app stopped working'. In the design mode I have checked the 'clickable' property and in 'onClick' I have also selected the 'onButtonClick'.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
     public void onButtonClick(View v)
     {
         int n1,n2,ans=0;
         EditText e1=(EditText) findViewById(R.id.num1);
         EditText e2=(EditText) findViewById(R.id.num2);
        TextView view_answer=(TextView) findViewById(R.id.answer);
         n1=Integer.parseInt(e1.getText().toString());
         n2=Integer.parseInt(e2.getText().toString());
        ans=n1+n2;
        view_answer.setText(Integer.toString (ans));
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}



<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add "
        android:id="@+id/add"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:nestedScrollingEnabled="false"
        android:/>

    <textview>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"

        android:id="@+id/answer"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp"
        android:width="123dp"
        android:removed="#ffcbffff" />

    <edittext>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/num1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/add"
        android:layout_alignEnd="@+id/add"
        android:layout_marginTop="45dp"
        android:width="65dp"
        android:removed="#fffeffa0" />

    <edittext>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/num2"
        android:width="65dp"
        android:removed="#ffffa6f0"
        android:layout_below="@+id/num1"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:textIsSelectable="true"
        android:height="45dp"
        android:layout_marginTop="88dp" />
</edittext></edittext></textview></relativelayout>


<resources>

    <string name="app_name">My Application</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="num1"></string>
    <string name="num2"></string>
    <string name="answer"></string>
    <string name="add">Add</string>

</resources>

解决方案

"Unfortunately", the activity_main.xml is in a mess, among other things, there are improperly formed tags, invalid attributes, and wrong letter case. Change to this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">
    <Button

        android:id="@+id/add"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerHorizontal="true"

        android:layout_centerVertical="true"

        android:/>
    <TextView

        android:id="@+id/answer"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentBottom="true"

        android:layout_centerHorizontal="true"

        android:layout_marginBottom="90dp"

        android:width="123dp"

        android:textAppearance="?android:attr/textAppearanceLarge" />
    <EditText

        android:id="@+id/num1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignEnd="@+id/add"

        android:layout_alignParentTop="true"

        android:layout_alignRight="@+id/add"

        android:layout_marginTop="45dp"

        android:width="65dp" />
    <EditText

        android:id="@+id/num2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_below="@+id/num1"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="88dp"

        android:width="65dp"

        android:height="45dp"

        android:textSize="20dp" />
</RelativeLayout>


The reason for not being able to edit "num2" edittext is the inclusion of

android:textIsSelectable="true"


Refer: Android UI Layouts and Controls
That should solve your problems.
It is highly recommended that you re-learn android development from these 12 winning articles at http://www.codeproject.com/Competitions/757/Android-Tutorial-Contest.aspx.


这篇关于为什么'不幸的是我的应用程序已停止'在Android模拟器中出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆