簡體   English   中英

Android App無法在模擬器中運行

[英]Android App Unable to run in emulator

我無法在模擬器上運行此應用程序。 這是一個顯示計數的簡單應用,可以使用按鈕增加或減少計數。 它可以正確安裝並可以正常打開,但是一旦按下按鈕,它就會異常關閉,顯示“不幸的是,應用程序已停止工作”。 我還將附加我的模擬器,環境詳細信息和應用程序代碼。

起點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類

<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>

應用啟動后的控制台

[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目標:Android 2.3.3 API等級:10內部存儲空間:200 MiB

另外,LogCat上沒有拋出任何錯誤,當我嘗試使用智能手機(lollypop 5.0)運行該應用程序時,響應是相同的。

請幫助,如果需要更多詳細信息,請告訴我。 謝謝。

您沒有初始化TextView ,請確保在setText()之前也將它改為FindViewById。

最重要的是,我認為您沒有初始化“ total” int變量。 當你做

total = total + 1;

它為null,因此崩潰

嘗試將total設置為0; 就在您的“ SetContentView”之后

在這里使用此代碼,這將起作用。

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