0

I'm trying to change the current activity from the main activity to another one by clicking a button, but then it won't work. When I click on the button it crashes the app and I have no idea why is it doing so. I have also tried to change the layout to the desired one on MainActivity.java and the emulator loaded the activity successfully so I think there's some problem on the method called when clicked.

MainActivity.java

package com.example.user.partyminigames;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

/* AppCompatActivity*/
public class MainActivity extends Activity {
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar); */

/*        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        }); */
    }

    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.button:
                intent.setClass(context, NumberGuessingActivity.class);
                startActivity(intent);
                break;
            }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

NumberGuessingActivity.java

package com.example.user.partyminigames;

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

/**
 * Created by USER on 26/12/2015.
 */
public class NumberGuessingActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numberguessing);

    }
}

AndroidManifest.xml

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NumberGuessingActivity">
            <intent-filter>
                <action android:name="number.guessing"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView android:text="@string/testing" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/asplode"
        android:id="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:autoText="false"
        android:textColor="@color/lose" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_numberGuessing"
        android:id="@+id/button"
        android:layout_marginTop="111dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="onClick" />
</RelativeLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
        android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

<!--        <android.support.v7.widget.Toolbar android:id="@+id/toolbar" -->
<!--            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" -->
<!--            android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> -->

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

<!--    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" -->
<!--        android:layout_width="wrap_content" android:layout_height="wrap_content" -->
<!--        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" -->
<!--        android:src="@android:drawable/ic_dialog_email" /> -->

</android.support.design.widget.CoordinatorLayout>

LogCat

12-27 01:25:49.436 1878-1878/? E/AndroidRuntime: FATAL EXCEPTION: main
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime: Process: com.example.user.partyminigames, PID: 1878
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3823)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4438)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18422)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3818)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4438) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18422) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.content.ComponentName.<init>(ComponentName.java:77)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.content.Intent.setClass(Intent.java:6360)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.example.user.partyminigames.MainActivity.onClick(MainActivity.java:39)
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$1.onClick(View.java:3818) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4438) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18422) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5001) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
12-27 01:25:49.436 1878-1878/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
12-27 01:25:49.456 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.456 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.456 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.466 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.466 360-404/? E/SoundPool: error loading /system/media/audio/ui/Effect_Tick.ogg
12-27 01:25:49.466 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressStandard.ogg
12-27 01:25:49.486 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressSpacebar.ogg
12-27 01:25:49.486 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressDelete.ogg
12-27 01:25:49.496 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressReturn.ogg
12-27 01:25:49.496 360-404/? E/SoundPool: error loading /system/media/audio/ui/KeypressInvalid.ogg

I'm trying to follow what the book taught me to code in Android but I'm stuck in changing activity. Hope the above codes/log gives enough info.

Edit: It's a null pointer exception but I couldn't find where tells me so in the log. Thanks for informing me about the very common null pointer exception though.

Jenny L.
  • 37
  • 9

4 Answers4

2

On MainActivity.java, the context variable was not initialized.

Change

intent.setClass(context, NumberGuessingActivity.class);

to

intent.setClass(this, NumberGuessingActivity.class);

and remove

 private Context context;
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • upvote for the explanation and presentation. – Kishor Pawar Dec 26 '15 at 18:01
  • Thanks so much! I've no idea on what I'm doing as I haven't even understood Android good enough from self-learning from a book. Do you have any idea what does that context suppose to do though? I try to copy codes from the examples to make things work. And I don't see the example uses `this` nor initialing the context – Jenny L. Dec 26 '15 at 18:04
  • Ah, I now see that I missed the line of `context = this`, silly me. – Jenny L. Dec 26 '15 at 18:17
0
public void onClick(View view) {
    Intent intent = new Intent(this,NumberGuessingActivity.class);
    startActivity(intent);
}
Lerul Ler
  • 339
  • 7
  • 21
0

You have your context null, i.e not initialized at

intent.setClass(context, NumberGuessingActivity.class);  

use

intent.setClass(this, NumberGuessingActivity.class);
Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61
0
  1. you have not initialized your context. So either in onCreate do this: context = this.getContext() or better just in your onClick() replace your

    intent.setClass(context, NumberGuessingActivity.class);

with

intent.setClass(this, NumberGuessingActivity.class);
  1. since you have set filters for NUmberGuessing activity in your manifest file, it would be better if you also set filters in your intent
Rusheel Jain
  • 843
  • 6
  • 20
  • Thanks there. Very new to Android and still understanding what's intent and other stuffs, may I ask how do you set filters in your intent? – Jenny L. Dec 26 '15 at 18:22
  • http://developer.android.com/reference/android/content/IntentFilter.html Here is a simplified explanation: http://stackoverflow.com/a/3322076/1750013 but my bad was that I did not see you have mentioned your filter as DEFAULT category. So in this case you do not need to change anything except fixing your null pointer – Rusheel Jain Dec 26 '15 at 19:22