简体   繁体   中英

The APK won't run on the android emulator. (Unfortunately the app has stopped)

I am new to android app development. I wrote an android code for a simple app that on clicking the Add one button, adds 1 to the total in the text field that's supposed to be above the button and on clicking subtract one button, subtracts 1 from the Text field above. Here is the code for it.

package com.example.ocrtransbybee;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;

public class StartingPoint extends Activity {

int counter;
Button add, sub;
TextView display;

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



    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (Button) findViewById(R.id.tvDisplay);

add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Your total is " + counter);
        }
    });
sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Your total is " + counter);
        }
    });

}

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

}

And the layout xml code is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".StartingPoint" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hey"
    android:textSize="50sp"
    android:layout_gravity="center"
    android:gravity="center"
    android:id="@+id/tvDisplay"
    />

<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/Button1"
    android:textSize="15sp"
    android:layout_gravity="center"
    android:gravity="center"
    android:id="@+id/bAdd"
    />
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/Button2"
    android:textSize="15sp"
    android:layout_gravity="center"
    android:gravity="center"
    android:id="@+id/bSub"
    />
    </LinearLayout>

And the runtime information:

[2014-02-16 20:57:49 - OCRTransByBee] New emulator found: emulator-5554
[2014-02-16 20:57:49 - OCRTransByBee] Waiting for HOME ('android.process.acore') to be launched...
[2014-02-16 21:00:41 - OCRTransByBee] HOME is up on device 'emulator-5554'

[2014-02-16 21:00:41 - OCRTransByBee] Uploading OCRTransByBee.apk onto device 'emulator-5554'

[2014-02-16 21:00:42 - OCRTransByBee] Installing OCRTransByBee.apk...

[2014-02-16 21:02:04 - OCRTransByBee] Success!

[2014-02-16 21:02:05 - OCRTransByBee] Starting activity com.example.ocrtransbybee.StartingPoint on device emulator-5554

[2014-02-16 21:02:11 - OCRTransByBee] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.ocrtransbybee/.StartingPoint }

It doesn't start in the emulator, the code seems fine to me, it was working fine in the school in my friend's laptop, it says "Unfortunately, the app has stopped" every time I try to run it.

You are casting TextView with a Button .

TextView display;

And you have

display = (Button) findViewById(R.id.tvDisplay);

Should be

display = (TextView) findViewById(R.id.tvDisplay);

You trying to cast display(TextView) object with Button Class.

Just replace

display = (Button) findViewById(R.id.tvDisplay);

with

display = (TextView) findViewById(R.id.tvDisplay);

There is cast class exception in code.

The problem is in this code of line.

display = (Button) findViewById(R.id.tvDisplay);

Change is to

display=  (TextView)findViewById(R.id.tvDisplay);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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