Big_Fan
Big_Fan

Reputation: 415

Button to new screen not working

I have searched and tried all the results, but to no avail. I am making a program that starts from the main screen and directs to new screens via buttons. I can get to the next screens through setContentView(R.layout.screen2); but my data from the java files connecting to the xml files never transfer. I do not think that the xml files are registering an association to the java files (as I even tried to create back buttons, and no matter what code I put, they would not go back to the previous screen). Intents make it so that the buttons don't even go to the next screen. I have been at this for a week now and I still don't know how to fix this problem.

Main Activity-

package com.wunapp.newsvideoapp;




import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;


public class MainActivity extends Activity {





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


        Button videoNext = (Button) findViewById(R.id.videoButton) ; 
        videoNext.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
              //setContentView(R.layout.screen2);



                  Intent i = new Intent(MainActivity.this, Screen2.class);
               // i.setClassName("rahul.application.WunApp", "rahul.application.WunApp.screen1");
                startActivity(i);
            }
        });

        Button newsNext = (Button) findViewById(R.id.newsButton);
        newsNext.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            //setContentView(R.layout.screen1);
            Intent intent = new Intent(MainActivity.this, Screen1.class);   
            startActivity(intent);

            }
        });



}

}

Screen2-

package com.wunapp.newsvideoapp;


//import java.io.IOException;


import java.io.File;

import android.net.Uri;

import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;

public class Screen2 extends Activity {


    VideoView videoView;
    @Override
  public void onCreate(Bundle savedInstanceState) 
    {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.screen2);

 Button back = (Button)this.findViewById(R.id.backButton);
        back.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
            setContentView(R.layout.activity_main);

            }
        });

...[code]
}
}

Main Activity xml-

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

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:text="@string/welcome" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </RelativeLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.25" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

        <Button
            android:id="@+id/videoButton"
            android:layout_width="156dp"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="@string/videoButton" />

        <Button
            android:id="@+id/newsButton"
            android:layout_width="152dp"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/videoButton"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="33dp"
            android:text="@string/newsButton" />

    </RelativeLayout>

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </RelativeLayout>

</LinearLayout>

screen2 xml-

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="fill_parent" 
    android:paddingLeft="2dp" android:paddingRight="2dp"
    android:paddingTop="2dp" android:paddingBottom="2dp"
    android:layout_width="fill_parent" android:orientation="vertical">


    <VideoView
        android:id="@+id/VideoView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/backButton" />





</RelativeLayout>

Manifest-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wunapp.newsvideoapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The app is a new/ video app, where you can get an RSS feed in screen1 , and videos in screen2... I left out all of the stuff that makes those work because I know that their codes work fine (tried them as main files in different projects). It is just the linking from screen to screen (and backwards in the case of the back button) that I need help with.

Upvotes: 0

Views: 332

Answers (4)

gsb
gsb

Reputation: 5640

The practice I follow to add a new activity is as below:

  1. Click on the AndroidManifest.xml.
  2. Click on the Application Nodes tab (bottom left of the screen)
  3. Click on the Add and select Activity as the option
  4. Click on the Name link and enter your activity name.

This adds a new activity to your project and also to the manifest.

Upvotes: 1

Nate
Nate

Reputation: 401

You need to add the other activities to your manifest

<activity
    android:name=".Screen1" android:label="Screen1Label" >
</activity>
<activity
    android:name=".Screen2" android:label="Screen2Label" >
</activity>

Upvotes: 1

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

change your manifest file like this

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  <activity
        android:name=".Screen2" />
</application>

Upvotes: 0

Romin
Romin

Reputation: 8816

Your AndroidManifest.xml indicates that there is only one main activity. You need to define both the Screen1 and Screen2 Activities in your manifest.

For example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wunapp.newsvideoapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Screen1"
            android:label="@string/title_activity_screen1" >
        </activity>
        <activity
            android:name=".Screen2"
            android:label="@string/title_activity_screen2" >
        </activity>
    </application>

</manifest>

Upvotes: 1

Related Questions