首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android应用程序无法在模拟器中运行

Android应用程序无法在模拟器中运行
EN

Stack Overflow用户
提问于 2015-09-21 20:18:07
回答 2查看 62关注 0票数 0

我无法在模拟器上运行这个应用程序。这是一个简单的应用程序,以显示计数,这可以增加或减少使用按钮。它安装正确,打开良好,但一旦按下按钮,它就会异常关闭,显示“不幸应用程序停止工作”。我正在附加我的模拟器,环境细节和应用程序代码。

启动Point.java类

代码语言:javascript
运行
复制
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类

代码语言:javascript
运行
复制
<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>

应用程序启动后的控制台

代码语言:javascript
运行
复制
[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 }

应用程序在此之后成功启动,但在应用程序意外关闭时显示错误。我也给我的仿真器下面的细节。

设备: Nexus 4目标:Android2.3.3API lvl: 10内部存储: 200 MiB

另外,在LogCat上没有抛出错误,当我尝试使用智能手机运行应用程序时,响应是相同的(棒棒糖5.0)。

请帮助,如果需要更多的细节,请告诉我。谢谢。

EN

Stack Overflow用户

回答已采纳

发布于 2015-09-21 20:22:50

您没有初始化您的TextView,请确保在setText()之前也要对其进行FindViewById。

更重要的是,我认为你没有初始化你的“总”int变量。当你做的时候

代码语言:javascript
运行
复制
total = total + 1;

它是空的,所以它崩溃了

试着在"SetContentView“之后设置total = 0;

票数 -1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32703860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档