Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
David
Člen
Avatar
David:9.11.2019 21:52

Ahoj, chtěl bych vytvořit jednoduchý program.
Postupoval jsem podle tohoto kódu: https://stackoverflow.com/…s-and-make-t

Nejdřív se spustí třída MyActivity s button. Když kliknu na button chtěl bych aby se mi načetla SecondActivity s textem: "Tohle je napsáno pomocí stringu v second aktivitě". To jsem zkusil jak pomocí second_layout i příkazem v SecondActivity. To se ale neděje. Po stisknutí tlačítka se mi nahoře sice ukáže SecondLayout, ale obsah zůstane z toho prvního tedy button, který však už nefunguje. Pak se ukáže že se aplikace stopne.
Nevíte kde by mohla být chyba?
Díky

Main Activity

package com.example.viceoken;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    //Define your views
    private Button Button;

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

        //Find your views
        Button = (Button) findViewById(R.id.button);

        //Assign a listener to your button
        Button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Start your second activity
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

SecondActivity

package com.example.viceoken;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

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

        TextView SecondLayout;

        SecondLayout = (TextView) findViewById(R.id.SecondLayout);
        SecondLayout.setText("@string/second_layout");


    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="112dp"
        android:layout_marginEnd="112dp"
        android:layout_marginBottom="191dp"
        android:text="@string/button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

second_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/SecondLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="320dp"
        android:layout_marginBottom="100dp"
        android:text="@string/second_layout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

MANIFEST

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.viceoken">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second_activitity" >
        </activity>
    </application>

</manifest>
 
Odpovědět
9.11.2019 21:52
Avatar
David
Člen
Avatar
David:9.11.2019 21:54

strings.xml

<resources>
    <string name="app_name">viceoken</string>
    <string name="button">button</string>
    <string name="title_activity_second_activitity">SecondActivity</string>
    <string name="second_layout">Tohle je napsáno pomocí stringu v second aktivitě</string>


</resources>
 
Nahoru Odpovědět
9.11.2019 21:54
Avatar
Odpovídá na David
Matúš Olejník:9.11.2019 22:01

Ahoj, začni s tým, že v SecondActivity nebudeš ako content view setovať activity_main layout ale second_layout

setContentView(R.layout.second_layout);
Akceptované řešení
+20 Zkušeností
+2,50 Kč
Řešení problému
Nahoru Odpovědět
9.11.2019 22:01
/* I am not sure why this works but it fixes the problem */
Avatar
David
Člen
Avatar
Odpovídá na Matúš Olejník
David:9.11.2019 22:24

Ajo :-) Dík

 
Nahoru Odpovědět
9.11.2019 22:24
Avatar
David
Člen
Avatar
David:9.11.2019 22:30

Ještě bych se chtěl zeptat jak bych mohl udělat ten nadpis co je v SecondActivity i u , MainActivity

 
Nahoru Odpovědět
9.11.2019 22:30
Avatar
Odpovídá na David
Matúš Olejník:9.11.2019 22:50

Myslím, že by mohlo stačiť ak namiesto Activity extendneš AppCompatActivity.

Nahoru Odpovědět
9.11.2019 22:50
/* I am not sure why this works but it fixes the problem */
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 6 zpráv z 6.