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
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
<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
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).
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.
On top of that, I think you didn't initialize your "total" int variable. When you do
total = total + 1;
Its null, so it crashes
Try setting total = 0; right after your "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);
}
});
}
}
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.