0

I have been messing around with this for a few days now and have the code identical to that in the tutorial and yet it still doesn't want to work, Why?

MainActivity.java

package com.example.mdpmk1;

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

public class MainActivity extends Activity {

Button button;

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

public void addListenerOnButton() {

    final Context context = this;

    button = (Button) findViewById(R.id.scan);

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(context, ScanScreen.class);
            startActivity(intent);   

            }

        });

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Screen One......"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/scan"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click me to another screen" />

</LinearLayout>

ScanScreen.java

package com.example.mdpmk1;

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

public class ScanScreen extends Activity {

Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scan_screen);
    }

}

scan_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="You have done it!!"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

AndroidMainfest.xml

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.firstapp.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>
</application>

</manifest>

Any help would be lovely, This has relay been annoying me and i know that it is probably a simple mistake.

Sparky
  • 21
  • 1
  • 6

3 Answers3

0

Add the following in your AndroidManifest.xml

<activity android:name=".ScanScreen" />

within application like so:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.firstapp.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=".ScanScreen" />
</application>

Here is a quote from the official docs on what the activity tags are for:

Declares an activity (an Activity subclass) that implements part of the application's visual user interface. All activities must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.

That last sentence is important.

singularhum
  • 5,072
  • 2
  • 24
  • 32
0

In your case writing <activity android:name=".ScanScreen" /> above </application> will solve your problem. Whenever you need to use any activity in android you have to mention it in AndroidManifest.xml file. Just giving answer to you won't help you. You should first read on below links to understand what is intent and intent-filters used for.

http://developer.android.com/reference/android/content/Intent.html http://developer.android.com/guide/components/intents-filters.html
What is an Intent in Android?

Community
  • 1
  • 1
baldguy
  • 2,090
  • 1
  • 16
  • 25
0

Your app crashes because your not setting or registered the activity in manifest.You haven't added the ScanScreen Activity in your manifest file. Just add the following to your AndroidManifest.xml :

<activity android:name=".ScanScreen" />

within your application tag like this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.firstapp.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=".ScanScreen" />
</application>
Pihu
  • 1,041
  • 11
  • 35