简体   繁体   English

Android App无法在模拟器中运行

[英]Android App Unable to run in emulator

I am unable to run this app on an emulator. 我无法在模拟器上运行此应用程序。 It's a simple app to display the count, which can be increased or decreased using buttons. 这是一个显示计数的简单应用,可以使用按钮增加或减少计数。 It installs correctly and opens fine but as soon as a button is pressed it closes abnormally displaying "Unfortunately App has stopped working". 它可以正确安装并可以正常打开,但是一旦按下按钮,它就会异常关闭,显示“不幸的是,应用程序已停止工作”。 I am attaching my emulator, environment detail and application code as well. 我还将附加我的模拟器,环境详细信息和应用程序代码。

Starting Point.java class 起点Point.java类

package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class StartingPoint extends Activity {

    int total;
    Button add,minus;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting_point);


        add=(Button)findViewById(R.id.addBtn);
        minus=(Button)findViewById(R.id.delBtn);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total+1;
                display.setText(" " +total);
            }
        });
        minus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total-1;
                display.setText(" " +total);
            }
        });
    }
}

StartingPoint.xml Class StartingPoint.xml类

<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="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/DisplayTV"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Your total is : "
        android:textSize="45dp" />

    <Button
        android:id="@+id/addBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/deleteBtn"
        android:layout_marginTop="163dp"
        android:text="ADD" />

    <Button
        android:id="@+id/delBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/DisplayTV"
        android:layout_marginTop="179dp"
        android:text="Delete" />

</RelativeLayout>

Console After App Launch 应用启动后的控制台

[2015-09-22 01:27:14 - Test] ------------------------------
[2015-09-22 01:27:14 - Test] Android Launch!
[2015-09-22 01:27:14 - Test] adb is running normally.
[2015-09-22 01:27:14 - Test] Performing com.example.test.StartingPoint activity launch
[2015-09-22 01:27:19 - Test] Uploading Test.apk onto device 'emulator-5554'
[2015-09-22 01:27:21 - Test] Installing Test.apk...
[2015-09-22 01:27:37 - Test] Success!
[2015-09-22 01:27:37 - Test] Starting activity com.example.test.StartingPoint on device emulator-5554
[2015-09-22 01:27:39 - Test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.test/.StartingPoint }

Application launches successfully after this but displays errors as the application closed unexpectedly. 此后,应用程序成功启动,但由于应用程序意外关闭而显示错误。 I am also giving my Emulator details below. 我还在下面提供我的模拟器详细信息。

Device: Nexus 4 Target: Android 2.3.3 API lvl: 10 Internal Storage: 200 MiB 设备:Nexus 4目标:Android 2.3.3 API等级:10内部存储空间:200 MiB

Also, there were no errors thrown on the LogCat, response was the same when I tried running the application using my smartphone (lollypop 5.0). 另外,LogCat上没有抛出任何错误,当我尝试使用智能手机(lollypop 5.0)运行该应用程序时,响应是相同的。

Please help, do let me know if more details are required. 请帮助,如果需要更多详细信息,请告诉我。 Thanks. 谢谢。

You didn't initialize your TextView , make sure to FindViewById it as well before you setText() it. 您没有初始化TextView ,请确保在setText()之前也将它改为FindViewById。

On top of that, I think you didn't initialize your "total" int variable. 最重要的是,我认为您没有初始化“ total” int变量。 When you do 当你做

total = total + 1;

Its null, so it crashes 它为null,因此崩溃

Try setting total = 0; 尝试将total设置为0; right after your "SetContentView" 就在您的“ SetContentView”之后

Here use this code, this will work. 在这里使用此代码,这将起作用。

public class StartingPoint extends Activity {

    int total=0;
    Button add,minus;
    TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting_point);
        display=(TextView)findViewById(R.id.DisplayTV);
        add=(Button)findViewById(R.id.addBtn);
        minus=(Button)findViewById(R.id.delBtn);
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total+1;
                display.setText(" " +total);
            }
        });
        minus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                total=total-1;
                display.setText(" " +total);
            }
        });
    }
}

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

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