06 Feb 2013, 22:39
Generic-user-small

samY Apps (2 posts)

I am new to ANDROID development.I followed the topics in “Hello,Android(3rd Edition)”. Until chapter 3.4 “Using Alternate Resources” it works fine… But in 3.5 “Implementing an About Box” i did all the steps given in the book. But when i try to run in simulator, it displays Unfortunately, Sudoku has stopped.. I don’t know what was wrong!

My activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" 
        android:background="@color/background"
        android:padding="30dip"
        android:layout_gravity="center"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/main_title" 
        android:layout_gravity="center"
        android:textSize="25sp"
        android:layout_marginBottom="25dip" />
    <Button
        android:id="@+id/continue_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/continue_label" />
    <Button 
        android:id="@+id/new_game_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/new_game_label" />
    <Button 
        android:id="@+id/about_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/about_label" />
    <Button 
        android:id="@+id/exit_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit_label" />
 
</LinearLayout>

my about.xml file

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:padding="30dip" >
    
	<TextView
	    android:id="@+id/about_content"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/about_text" />

</ScrollView>

my strings.xml file

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Sudoku</string>
    <string name="main_title">SUDOKU</string>
    <string name="menu_settings">Settings</string>
    <string name="continue_label">Continue</string>
    <string name="new_game_label">New Game</string>
    <string name="about_label">About</string>
    <string name="exit_label">Exit</string>
    <string name="about_title">About Sudoku</string>
    <string name="about_text">\Sudoku is a logic-based number placement puzzle.
		Starting with a partially completed 9x9 grid, the
		objective is to fill the grid so that each
		row, each column, and each of the 3x3 boxes
		(also called blocks) contains the digits
		1 to 9 exactly once.</string>

</resources>

my MainActivity.java file

package org.example.sudoku;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// Set up click listeners for all the buttons
		View continueButton = findViewById(R.id.continue_button);
		continueButton.setOnClickListener(this);
		View newButton = findViewById(R.id.new_button);
		newButton.setOnClickListener(this);
		View aboutButton = findViewById(R.id.about_button);
		aboutButton.setOnClickListener(this);
		View exitButton = findViewById(R.id.exit_button);
		exitButton.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.about_button:
		Intent i = new Intent(this, About.class);
		startActivity(i);
		break;
		// More buttons go here (if any) ...
		}
		}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

my About.java file

package org.example.sudoku;

import android.app.Activity;
import android.os.Bundle;

public class About extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.about);
	}

}

my AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.example.sudoku"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="org.example.sudoku.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name="org.example.sudoku.About"
			android:label="@string/about_title" >
		</activity>
    </application>

</manifest>

my log file

02-07 02:37:39.678: W/Trace(787): Unexpected value from nativeGetEnabledTags: 0
02-07 02:37:39.698: W/Trace(787): Unexpected value from nativeGetEnabledTags: 0
02-07 02:37:40.518: D/AndroidRuntime(787): Shutting down VM
02-07 02:37:40.538: W/dalvikvm(787): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-07 02:37:40.568: E/AndroidRuntime(787): FATAL EXCEPTION: main
02-07 02:37:40.568: E/AndroidRuntime(787): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.sudoku/org.example.sudoku.MainActivity}: java.lang.NullPointerException
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.os.Looper.loop(Looper.java:137)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at java.lang.reflect.Method.invokeNative(Native Method)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at java.lang.reflect.Method.invoke(Method.java:511)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at dalvik.system.NativeStart.main(Native Method)
02-07 02:37:40.568: E/AndroidRuntime(787): Caused by: java.lang.NullPointerException
02-07 02:37:40.568: E/AndroidRuntime(787): 	at org.example.sudoku.MainActivity.onCreate(MainActivity.java:19)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.Activity.performCreate(Activity.java:5104)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-07 02:37:40.568: E/AndroidRuntime(787): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-07 02:37:40.568: E/AndroidRuntime(787): 	... 11 more
02-07 02:38:54.168: I/Process(787): Sending signal. PID: 787 SIG: 9

Help me to solve my problem….Thanks in advance….

07 Feb 2013, 08:24
Generic-user-small

Anthony Shaw (136 posts)

The big clue is in the log file after the line ‘Caused by: java.lang.NullPointerException’, it tells you the problem is in the onCreate method in MainActivity.java at line 19. If we look at line 19 we see

newButton.setOnClickListener(this);

This would indicate that newButton has not been initialised. Looking for where newButton is initialised takes us to line 18

View newButton = findViewById(R.id.new_button);

This look OK, so checking where R.id.new_button comes from in the layout we find it’s not there but there is a new_game_button. So I would suggest changing line 18 to

View newButton = findViewById(R.id.new_game_button);

Hope that helps.

18 Feb 2013, 19:09
Generic-user-small

samY Apps (2 posts)

Thanks Anthony Shaw… :) It works….

14 Apr 2013, 02:17
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Anthony did a good job describing not just the solution, but how to find it for yourself next time.

21 Jul 2013, 12:00
Generic-user-small

Ian Kinny (1 post)

I am also new to android. When Ed said do stuff in Sukoku.java I created a class and put the code in it. It didn’t work. Lots of the ebook refers to Sudoku.java. It might be just me but I found it confusing. I was so pleased to find this post as it ended several frustrating hours with an (obvious) fix.

The ebook also refers to main instead of activity_main and this tends to be confusing at first as well.

I am very pleased with the book otherwise (an update would be good)

  You must be logged in to comment