0

I'm new to computer programming. I am self-learning Java, so please be easy on me. I may not understand all the technical terms you use. I would prefer a straight out re-written code solution, but if you decide to just explain what the problem is, and how to fix it, please explain it to me like I'm a 5th grader.

To put it simply, I am trying to create an Android App such that on its MainActivity, there is an image button. When the user taps the image button it should take him to a new activity I have called NewActivity. However, when I run the app on a Nexus 6 emulator it crashes when I click the image button.

The code that I have inserted into Main Activity is this:

package com.example.matthew.test1;

import android.content.Intent;
import android.media.Image;
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;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity {
//Main Menu: MarioActivity1 to MarioActivity2
    ImageButton myImageButton;

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

        myImageButton= (ImageButton) findViewById(R.id.myImageButton);

        myImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentLoadNewActivity = new Intent(MainActivity.this, NewActivity.class);
                startActivity(intentLoadNewActivity);

            }
        });
    }
}
//End

Now the app actually works fine with this code on its own, since I got it from a tutorial. Basically, I am successful in getting an App to have an image button that leads to a new activity. The problem arose when I took code from another tutorial.

The tutorial tries to create a sort of "slideshow". What I mean is the code attempts to create an image in the background, and two buttons (Prev and Next). The first image loaded should be Image1 (for example), then if you click Next it should show Image2, or if you press Prev from Image2 it should go back to Image1, etc.

Now, although the tutorial applied code to MainActivity (i.e. MainActivity.java and activity_main.xml), I am trying to apply the code to my NewActivity.

Thus, here is the code that I inserted into activity_new.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.matthew.test1.NewActivity">

    <ViewFlipper
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/viewFlipper">

    </ViewFlipper>

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView"
            android:scaleType="centerCrop"
            android:src="@drawable/mario_bair_1"/>

        <ImageView
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:id="@+id/ImageView2"
             android:scaleType="centerCrop"
             android:src="@drawable/mario_bair_2"/>

         <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ImageView3"
            android:scaleType="centerCrop"
            android:src="@drawable/mario_bair_3"/>

    <Button
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

    <Button
        android:id="@+id/previous"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prev"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignTop="@+id/next"/>

</RelativeLayout>

And here is the code I have in NewActivity.java:

package com.example.matthew.test1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;


public class NewActivity extends AppCompatActivity implements View.OnClickListener {

    ViewFlipper viewFlipper;
    Button next;
    Button previous;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
        next = (Button) findViewById(R.id.next);
        previous = (Button) findViewById(R.id.previous);

        next.setOnClickListener(this);
        previous.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        if (v == next) {
            viewFlipper.showNext();
        }
        else if (v == previous) {
            viewFlipper.showPrevious();
        }
    }
}

Finally, here is the error log as seen in Android Monitor:

05-16 13:20:28.023 19225-19225/com.example.matthew.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.matthew.test1-2/lib/x86
05-16 13:20:28.640 19225-19225/com.example.matthew.test1 W/System: ClassLoader referenced unknown path: /data/app/com.example.matthew.test1-2/lib/x86
05-16 13:20:29.690 19225-19225/com.example.matthew.test1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-16 13:20:29.702 19225-19235/com.example.matthew.test1 I/art: Background sticky concurrent mark sweep GC freed 8434(1445KB) AllocSpace objects, 1(20KB) LOS objects, 71% free, 1064KB/3MB, paused 9.462ms total 71.078ms
05-16 13:20:29.872 19225-19277/com.example.matthew.test1 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                           [ 05-16 13:20:29.876 19225:19225 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xaaa153d0, tid 19225


                                                                           [ 05-16 13:20:29.965 19225:19277 D/         ]
                                                                           HostConnection::get() New Host Connection established 0xaaa15670, tid 19277
05-16 13:20:29.972 19225-19277/com.example.matthew.test1 I/OpenGLRenderer: Initialized EGL, version 1.4
05-16 13:20:30.040 19225-19277/com.example.matthew.test1 W/EGL_emulation: eglSurfaceAttrib not implemented
05-16 13:20:30.040 19225-19277/com.example.matthew.test1 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaa32600, error=EGL_SUCCESS
05-16 13:20:39.924 19225-19225/com.example.matthew.test1 D/AndroidRuntime: Shutting down VM
05-16 13:20:39.924 19225-19225/com.example.matthew.test1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.matthew.test1, PID: 19225
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matthew.test1/com.example.matthew.test1.NewActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                               at com.example.matthew.test1.NewActivity.onCreate(NewActivity.java:24)
                                                                               at android.app.Activity.performCreate(Activity.java:6237)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-16 13:20:40.998 19225-19231/com.example.matthew.test1 W/art: Suspending all threads took: 5.032ms
05-16 13:20:42.104 19225-19225/com.example.matthew.test1 I/Process: Sending signal. PID: 19225 SIG: 9

The code taken from the tutorial for the slideshow is successful on its own as shown in the tutorial. It is when I combine it with my code in MainActivity that the problem seems to arise.

Again, I basically just want the App not to force close (which it is currently doing) when I click the button in MainActivity that leads to NewActivity.

Thanks for your help in advance!

  • Your problem is likely that you set the contentView to activity_main which likely does not contain your next and previous buttons. The object(s) are null after the find. The layout you listed is called activity_new, and your new activity does not use it. – Gary Bak May 16 '16 at 18:16
  • @GaryBak That fixed it. Thank you!!! – mercenariez May 16 '16 at 18:22

0 Answers0