mitchnufc
mitchnufc

Reputation: 309

Android OnClick error

I am currently working through a Java Android book and have hit a problem. I am creating a basic sudoku app and am trying to add a OnClickListener so that when someone hits the 'About' button it displays text, whenever this button is hit I just get the error message as follows. 'The application Sudoku has stopped, please try again.

Here is my code

Manifest 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" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".Sudoku"
            android:label="@string/app_name" >

        <activity 
            android:name=".About"
            android:label="@string/about_title">
         </activity>   

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
      </activity>

     </application>

</manifest>

I have an about class

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);
    }

}

and lastly is my Sudoku java class

package org.example.sudoku;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.View.OnClickListener;

public class Sudoku extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //set up listeners for 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);



    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.about_button:
            Intent i = new Intent(this, About.class);
            startActivity(i);
            break;
        }

    }
}

Thanks for looking at this and I'm using eclipse by the way, tryed using the Logcat and De-Bugger but no luck.

LogCat errors

02-13 20:33:08.376: E/AndroidRuntime(524): FATAL EXCEPTION: main
02-13 20:33:08.376: E/AndroidRuntime(524): android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.sudoku/org.example.sudoku.About}; have you declared this activity in your AndroidManifest.xml?
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.app.Activity.startActivityForResult(Activity.java:3190)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.app.Activity.startActivity(Activity.java:3297)
02-13 20:33:08.376: E/AndroidRuntime(524):  at org.example.sudoku.Sudoku.onClick(Sudoku.java:34)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.view.View.performClick(View.java:3511)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.view.View$PerformClick.run(View.java:14105)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.os.Handler.handleCallback(Handler.java:605)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.os.Looper.loop(Looper.java:137)
02-13 20:33:08.376: E/AndroidRuntime(524):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-13 20:33:08.376: E/AndroidRuntime(524):  at java.lang.reflect.Method.invokeNative(Native Method)
02-13 20:33:08.376: E/AndroidRuntime(524):  at java.lang.reflect.Method.invoke(Method.java:511)
02-13 20:33:08.376: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-13 20:33:08.376: E/AndroidRuntime(524):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-13 20:33:08.376: E/AndroidRuntime(524):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1608

Answers (4)

Jeremy D
Jeremy D

Reputation: 4855

<activity
            android:name=".Sudoku"
            android:label="@string/app_name" >

        <activity 
            android:name=".About"
            android:label="@string/about_title">
         </activity>   

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
      </activity>

should be :

<activity>
       android:name=".Sudoku"
       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=".About"
       android:label="@string/about_title">
  </activity> 

Upvotes: 0

waqaslam
waqaslam

Reputation: 68177

you have defined activity wrong in manifest. A part of it should be as below:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".Sudoku"
            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=".About"
            android:label="@string/about_title">
         </activity>  

     </application>

Upvotes: 1

Basic Coder
Basic Coder

Reputation: 11422

Button aboutButton = (Button)findViewById(R.id.about_button);
if (aboutButton != null)
    aboutButton.setOnClickListener(this);
else
    Log.e("MyTag", "aboutButton not found on View");

Also make sure to edit your manifest.

<?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" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".Sudoku"
            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=".About"
            android:label="@string/about_title">
         </activity>  

     </application>

</manifest>

This should work.

Upvotes: 0

Selkie
Selkie

Reputation: 1783

I'm not sure if it's the reason, but ur manifest file seems messed up. Those two activity tags are mingled together. You should change the application part to:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".Sudoku"
        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=".About"
        android:label="@string/about_title">
    </activity>

 </application>

Upvotes: 1

Related Questions