1

I'm new to android programming and I was creating a simple app from a tutorial when I got an error on debug. In the tutorial the app contains a button that counts how many times it's pressed, and that project works, but I edited the project to create an app with two buttons, one button counts how many times it's pressed (with a simple if-else) and other reset the counting. This modified app doesn't work.

These are code errors and code files:

Errors:

11-25 21:47:32.888: E/AndroidRuntime(6865): FATAL EXCEPTION: main
11-25 21:47:32.888: E/AndroidRuntime(6865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flavio.tictactoe/com.flavio.tictactoe.TicTacToe}: java.lang.NullPointerException
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.os.Looper.loop(Looper.java:137)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.main(ActivityThread.java:4514)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at java.lang.reflect.Method.invokeNative(Native Method)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at java.lang.reflect.Method.invoke(Method.java:511)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at dalvik.system.NativeStart.main(Native Method)
11-25 21:47:32.888: E/AndroidRuntime(6865): Caused by: java.lang.NullPointerException
11-25 21:47:32.888: E/AndroidRuntime(6865):     at com.flavio.tictactoe.TicTacToe.onCreate(TicTacToe.java:24)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.Activity.performCreate(Activity.java:4465)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-25 21:47:32.888: E/AndroidRuntime(6865):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-25 21:47:32.888: E/AndroidRuntime(6865):     ... 11 more

TicTacToe.java(yes, I know it isn't a tic tac toe app :) ):

package com.flavio.tictactoe;

import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;
import android.view.View;

public class TicTacToe extends Activity 
implements View.OnClickListener{

Button button;
int touchCount;
String number;
Button ResetButton;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
button = new Button(this);
button=((Button)this.findViewById(R.id.button_count));
button.setText( "Touch me!" );
button.setOnClickListener(this);
ResetButton=new Button(this);
ResetButton=((Button)this.findViewById(R.id.button_reset));
ResetButton.setText("Reset");
ResetButton.setOnClickListener(this);
setContentView(R.id.button_count);
setContentView(R.id.button_reset);

}


public void onClick(View v) {
    switch(v.getId()){

    case R.id.button_count:
        touchCount++;
        if(touchCount>1){
            number=" times";
            }else{
                number=" time";
                }

        button.setText("Touched me " + touchCount + number);
    case R.id.button_reset:
        touchCount=0;
        if(touchCount>1){
            number=" times";
            }else{
                number=" time";
                }

    }
    }
}

activity_tic_tac_toe.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=".TicTacToe" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button_count"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

    <Button
        android:id="@+id/button_reset"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"/>
</RelativeLayout>

P.S. I have got a problem with android.jar file: every time I want to see a .class file, eclipse tells me 'Source not found'

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
moonknight
  • 334
  • 1
  • 7

1 Answers1

2

You need to use setContentView() to display your activity_tic_tac_toe layout first. Then you can use findViewById() to fetch the Buttons, or any other View, within this layout:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tic_tac_toe); // The layout that holds your Buttons

button=((Button)this.findViewById(R.id.button_count));
button.setText( "Touch me!" );
button.setOnClickListener(this);

ResetButton=((Button)this.findViewById(R.id.button_reset));
ResetButton.setText("Reset");
ResetButton.setOnClickListener(this);

(Notice that I removed some unnecessary lines.)


P.S. I have got a problem with android.jar file: every time I want to see a .class file, eclipse tells me 'Source not found'

Are you trying to look through the source code? Click Attach Source Code and point it towards your copy of the Android SDK.

If you don't have that option, read this question: How to attach javadoc or sources to jars in libs folder?

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179