• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Changing Views

Radicool

Lurker
Jan 15, 2016
4
0
I've only learned basic C++, so I know very little about java and android development. I'm creating an application that just changes the "view" (switches between .xml layouts). That's all I'm trying to do for now.

The problem is my application won't run. If I comment out the second "onclicklistener" block of code it will run though. Whats the problem here?

Code:
package com.mycompany.multipleviews;

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.Button; // For Button widgets
import android.util.Log; // Needed for creating logs

public class MainActivity extends AppCompatActivity {

    // Create button/display objects
    Button page1Button, page2Button;

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

        Log.d("Test", "App Created");

        // Initiate button/display objects
        page1Button = (Button) findViewById(R.id.Page1Button);
        page2Button = (Button) findViewById(R.id.Page2Button);

        page1Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //do something
                //Log.d("Test", "Button has been pressed");
                setContentView(R.layout.layout2);

            }
        });

        page2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //do something
                //Log.d("Test", "Button has been pressed");
                setContentView(R.layout.layout3);

            }
        });

    }
}
 
I try to run the application directly on my phone. It begins loading on my phone then returns to the home screen with the message:

Unfortunately, Multiple Views has stopped.

Here is the info in the "logcat". I'm unable to make much sense of this though. My old C++ programming in Visual Studio would give warnings and compile errors and tell me which lines were causing problems. Here I'm lost!



01-15 21:40:03.425 13220-13220/? D/dalvikvm: Late-enabling CheckJNI
01-15 21:40:03.590 13220-13220/com.mycompany.multipleviews W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
01-15 21:40:03.590 13220-13220/com.mycompany.multipleviews I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
01-15 21:40:03.595 13220-13220/com.mycompany.multipleviews W/dalvikvm: VFY: unable to resolve interface method 17895: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
01-15 21:40:03.595 13220-13220/com.mycompany.multipleviews D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
01-15 21:40:03.595 13220-13220/com.mycompany.multipleviews I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
01-15 21:40:03.595 13220-13220/com.mycompany.multipleviews W/dalvikvm: VFY: unable to resolve interface method 17899: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
01-15 21:40:03.595 13220-13220/com.mycompany.multipleviews D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
01-15 21:40:03.650 13220-13220/com.mycompany.multipleviews D/Test: App Created
01-15 21:40:03.650 13220-13220/com.mycompany.multipleviews D/AndroidRuntime: Shutting down VM
01-15 21:40:03.650 13220-13220/com.mycompany.multipleviews W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41e9a2a0)
01-15 21:40:03.655 13220-13220/com.mycompany.multipleviews E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.multipleviews/com.mycompany.multipleviews.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.mycompany.multipleviews.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)

Maybe if anyone had a link to explain common android errors would also help me.
 
Upvote 0
Here is the xml code for layout1 and layout2 (added space after colon to prevent smilies being drawn):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android: orientation="horizontal"
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">

<TextView
android:text="We're going to..."
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:text="Page 1"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="370dp"/>

<Button
android:id="@+id/Page1Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="400dp"
android:text="Next Page"/>


</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android: orientation="horizontal"
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">

<TextView
android:text="change the view..."
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:text="Page 2"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="370dp"/>

<Button
android:id="@+id/Page2Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="400dp"
android:text="Next Page"/>


</RelativeLayout>

I think the problem is I'm trying to initialize the button2 object, but that button is in the layout2 .xml. Perhaps the program is expected this button2 to be in layout1 and hence cannot find it? I changed my code to this:

Code:
package com.mycompany.multipleviews;

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.Button; // For Button widgets
import android.util.Log; // Needed for creating logs

public class MainActivity extends AppCompatActivity {

    // Create button/display objects
    Button page1Button, page2Button;

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

        Log.d("Test", "App Created");

        // Initiate button/display objects
        page1Button = (Button) findViewById(R.id.Page1Button);


        page1Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //do something
                //Log.d("Test", "Button has been pressed");
                setContentView(R.layout.layout2);
                // Initiate button/display objects for next page
                page2Button = (Button) findViewById(R.id.Page2Button);

            }
        });

        page2Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //do something
                //Log.d("Test", "Button has been pressed");
                setContentView(R.layout.layout3);

            }
        });

    }
}

But it still hasn't worked. Any ideas what I'm doing wrong?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones