Question

I'm new to Android development and trying to build my first application. I'm currently trying to build a simple counter that allows the user to increment the total by +1, -1, +5, -5 with the starting value being 20. When I try to run my app it always crashes immediately and I'm completely stuck on how to fix it.

Here is my code:

package com.example.mtglifecounter;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

int Total=20;
Button Plus1, Min1, Plus5, Min5;
EditText Display;

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

    //Total = 20;
    Plus1 = (Button) findViewById(R.id.btnPlus1);
    Min1 = (Button) findViewById(R.id.btnMin1);
    Plus5 = (Button) findViewById(R.id.btnPlus5);
    Min5 = (Button) findViewById(R.id.btnmin5);
    Display = (EditText) findViewById(R.id.tvTotal);
    Display.setText(Total);

    Plus1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Adds 1 to the counter
            Total = Total + 1;
            Display.setText(Total);
        }
    });

    Min1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Subtract 1 from counter
            Total = Total - 1;
            Display.setText(Total);
        }
    });

    Plus5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Adds 1 to the counter
            Total = Total + 5;
            Display.setText(Total);
        }
    });

    Min5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Subtract 1 from counter
            Total = Total - 5;
            Display.setText(Total);
        }
    });

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

I also have a logcat that I saved:

04-11 11:56:27.433: D/AndroidRuntime(1459): Shutting down VM
04-11 11:56:27.433: W/dalvikvm(1459): threadid=1: thread exiting with uncaught exception (group=0xb1aabba8)
04-11 11:56:27.453: E/AndroidRuntime(1459): FATAL EXCEPTION: main
04-11 11:56:27.453: E/AndroidRuntime(1459): Process: com.example.mtglifecounter, PID: 1459
04-11 11:56:27.453: E/AndroidRuntime(1459): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mtglifecounter/com.example.mtglifecounter.MainActivity}: java.lang.NullPointerException
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.os.Looper.loop(Looper.java:136)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at java.lang.reflect.Method.invoke(Method.java:515)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at dalvik.system.NativeStart.main(Native Method)
04-11 11:56:27.453: E/AndroidRuntime(1459): Caused by: java.lang.NullPointerException
04-11 11:56:27.453: E/AndroidRuntime(1459):     at com.example.mtglifecounter.MainActivity.onCreate(MainActivity.java:34)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.Activity.performCreate(Activity.java:5231)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 11:56:27.453: E/AndroidRuntime(1459):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 11:56:27.453: E/AndroidRuntime(1459):     ... 11 more
04-11 11:56:33.373: I/Process(1459): Sending signal. PID: 1459 SIG: 9

I would really appreciate any help you can give me since this is my first try at Android.

Thanks

Here is the activity_Main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mtglifecounter.MainActivity"
tools:ignore="MergeRootFrame" >

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mtglifecounter.MainActivity$PlaceholderFragment" >

<EditText
    android:id="@+id/etTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/LifeTotal"
    android:ems="10"
    android:inputType="number"
    android:text="20" />

<Button
    android:id="@+id/btnmin5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnPlus5"
    android:layout_alignBottom="@+id/btnPlus5"
    android:layout_alignLeft="@+id/btnMin1"
    android:onClick="On_Clicked"
    android:text="-5" />

<Button
    android:id="@+id/btnMin1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnPlus1"
    android:layout_alignBottom="@+id/btnPlus1"
    android:layout_alignLeft="@+id/LifeTotal"
    android:layout_marginLeft="37dp"
    android:onClick="On_Clicked"
    android:text="-1" />

<Button
    android:id="@+id/btnPlus5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/tvTotal"
    android:layout_below="@+id/btnPlus1"
    android:layout_marginRight="60dp"
    android:layout_marginTop="25dp"
    android:onClick="On_Clicked"
    android:text="+5" />

<Button
    android:id="@+id/btnPlus1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/btnPlus5"
    android:layout_below="@+id/LifeTotal"
    android:layout_marginTop="44dp"
    android:onClick="On_Clicked"
    android:text="+1" />

<TextView
    android:id="@+id/LifeTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/tvTotal"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="21dp"
    android:text="Life Total:"
    android:textSize="@dimen/abc_action_bar_title_text_size" />

Was it helpful?

Solution

As per your code and layout , issue is with the EditText Id.

You are trying to find the EditText view using the Button Id

As per your layout change this line

Display = (EditText) findViewById(R.id.tvTotal);

to

Display = (EditText) findViewById(R.id.etTotal);

Also , the other issue is in setText to EditText

Display.setText(Total);

Total is an int. It should be a String or should be a valid String Resource ID

If you are setting an integer , it will be considered as Resource Id

This what the setText method does...

public final void setText(int resid) {
    setText(getContext().getResources().getText(resid));
}

Either, change the int to pass the correct string Resource Id or convert the int to string like this

Display.setText(Integer.toString(Total)); 

OTHER TIPS

You should use a debugger. Step each line in your onCreate method until you see a value that is wrong - probably a null pointer. You don't say how you built your application but using both Eclipse or Android Studio allow line by line debug stepping. Use this feature and it will allow you to find your bugs quickly and easily.

https://developer.android.com/tools/debugging/debugging-projects.html

make sure all of these exists in your layout activity_mail.xml

Plus1 = (Button) findViewById(R.id.btnPlus1);
Min1 = (Button) findViewById(R.id.btnMin1);
Plus5 = (Button) findViewById(R.id.btnPlus5);
Min5 = (Button) findViewById(R.id.btnmin5);
Display = (EditText) findViewById(R.id.tvTotal);

And by that I mean, check if there is a Button with ID btnPlus1 , then check if there is a Button with ID btnMin1, and so on.

Since the exception is a NullPointerException the problem is probably that you mistyped an ID and one (or more) of them is returning null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top