1. Are you ready for the Galaxy S20? Here is everything we know so far!

Changing Views

Discussion in 'Android Development' started by Radicool, Jan 15, 2016.

  1. Radicool

    Radicool Lurker
    Thread Starter

    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 (Text):
    1. package com.mycompany.multipleviews;
    2.  
    3. import android.os.Bundle;
    4. import android.support.design.widget.FloatingActionButton;
    5. import android.support.design.widget.Snackbar;
    6. import android.support.v7.app.AppCompatActivity;
    7. import android.support.v7.widget.Toolbar;
    8. import android.view.View;
    9. import android.view.Menu;
    10. import android.view.MenuItem;
    11.  
    12. import android.widget.Button; // For Button widgets
    13. import android.util.Log; // Needed for creating logs
    14.  
    15. public class MainActivity extends AppCompatActivity {
    16.  
    17.     // Create button/display objects
    18.     Button page1Button, page2Button;
    19.  
    20.     @Override
    21.     protected void onCreate(Bundle savedInstanceState) {
    22.         super.onCreate(savedInstanceState);
    23.         setContentView(R.layout.layout1);
    24.  
    25.         Log.d("Test", "App Created");
    26.  
    27.         // Initiate button/display objects
    28.         page1Button = (Button) findViewById(R.id.Page1Button);
    29.         page2Button = (Button) findViewById(R.id.Page2Button);
    30.  
    31.         page1Button.setOnClickListener(new View.OnClickListener() {
    32.             @Override
    33.             public void onClick(View view) {
    34.  
    35.                 //do something
    36.                 //Log.d("Test", "Button has been pressed");
    37.                 setContentView(R.layout.layout2);
    38.  
    39.             }
    40.         });
    41.  
    42.         page2Button.setOnClickListener(new View.OnClickListener() {
    43.             @Override
    44.             public void onClick(View view) {
    45.  
    46.                 //do something
    47.                 //Log.d("Test", "Button has been pressed");
    48.                 setContentView(R.layout.layout3);
    49.  
    50.             }
    51.         });
    52.  
    53.     }
    54. }
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Deleted User

    Deleted User Guest

    Does the application crash? Do you get an exception?
     
  3. Radicool

    Radicool Lurker
    Thread Starter

    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.
     
  4. Deleted User

    Deleted User Guest

    Hint: page2Button is probably null. You probably haven't included the button view in your layout, but without seeing the XML for layout1 I can't say for sure.
     
  5. Radicool

    Radicool Lurker
    Thread Starter

    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 (Text):
    1.  
    2. package com.mycompany.multipleviews;
    3.  
    4. import android.os.Bundle;
    5. import android.support.design.widget.FloatingActionButton;
    6. import android.support.design.widget.Snackbar;
    7. import android.support.v7.app.AppCompatActivity;
    8. import android.support.v7.widget.Toolbar;
    9. import android.view.View;
    10. import android.view.Menu;
    11. import android.view.MenuItem;
    12.  
    13. import android.widget.Button; // For Button widgets
    14. import android.util.Log; // Needed for creating logs
    15.  
    16. public class MainActivity extends AppCompatActivity {
    17.  
    18.     // Create button/display objects
    19.     Button page1Button, page2Button;
    20.  
    21.     @Override
    22.     protected void onCreate(Bundle savedInstanceState) {
    23.         super.onCreate(savedInstanceState);
    24.         setContentView(R.layout.layout1);
    25.  
    26.         Log.d("Test", "App Created");
    27.  
    28.         // Initiate button/display objects
    29.         page1Button = (Button) findViewById(R.id.Page1Button);
    30.  
    31.  
    32.         page1Button.setOnClickListener(new View.OnClickListener() {
    33.             @Override
    34.             public void onClick(View view) {
    35.  
    36.                 //do something
    37.                 //Log.d("Test", "Button has been pressed");
    38.                 setContentView(R.layout.layout2);
    39.                 // Initiate button/display objects for next page
    40.                 page2Button = (Button) findViewById(R.id.Page2Button);
    41.  
    42.             }
    43.         });
    44.  
    45.         page2Button.setOnClickListener(new View.OnClickListener() {
    46.             @Override
    47.             public void onClick(View view) {
    48.  
    49.                 //do something
    50.                 //Log.d("Test", "Button has been pressed");
    51.                 setContentView(R.layout.layout3);
    52.  
    53.             }
    54.         });
    55.  
    56.     }
    57. }
    58.  

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

    Radicool Lurker
    Thread Starter

    Ok thanks, I'll look into this when I get a chance.
     
Loading...

Share This Page

Loading...