Home > Net >  Android studio - items in second activity not showing
Android studio - items in second activity not showing

Time:02-28

The button and two TextViews will not show up when I navigate to the second activity. They do show up in Design view. I've looked at similar articles but cannot figure out the issue. I'm using Java. Thank you

Main.java

package com.example.cybersecgame;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SecondActivity.class));
            }
        });
    }
}

SecondActivity.java

package com.example.cybersecgame;

import static com.example.cybersecgame.R.layout.activity_second;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_second);
    }
}

CodePudding user response:

I don't understand the use of this line

import static com.example.cybersecgame.R.layout.activity_second;

remove it, and when setting the content make sure you use R.layout.'layout name', as R.layoout.... refers to you reading the layout file from the drawable layout folder

so it shall be like so

setContentView(R.layout.activity_second);

CodePudding user response:

According to Android doc, setContentView:

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.

This means that it loads this new view, de facto overwriting all the elements of the previous activity. From what I understand, to preserve the button and the textviews, it would suffice to add them to the second activity.

<Button
    android:id="@ id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Change activity" />

<TextView
    android:id="@ id/simpleTextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button"
    android:text="Text One" />

<TextView
    android:id="@ id/simpleTextView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/simpleTextView1"
    android:text="Text Two" />

Or simply, using the same layout for the second activity:

setContentView(R.layout.activity_main);

instead of

setContentView(R.layout.activity_second);

Another way to do this could be by using the calling Intent's putExtra method.

  • Related