I did some Android studio tutorials and made an App with Kotlin. On 1 screen all works as I like but open a 2nd screen via a button wont work

huangapple go评论42阅读模式
英文:

I did some Android studio tutorials and made an App with Kotlin. On 1 screen all works as I like but open a 2nd screen via a button wont work

问题

以下是您要翻译的内容:

要通过第一个屏幕上的按钮访问第二个屏幕,我按照互联网教程进行了操作,所以我插入了:

  • 第二个活动和
  • 第二个布局。

在第二个布局中,我添加了一个textView,
在第一个活动中,我添加了一个按钮:

package com.example.inko_gnito

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {
private lateinit var startbutton: Button
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_screen)
    startbutton = findViewById(R.id.startbutton)
    startbutton.setOnClickListener {
        val intent = Intent(this, Playerselect::class.java)
        startActivity(intent)
    }
}
}`

当我运行第一个活动时,一切都正常。当我点击按钮访问第二个屏幕时,第二个屏幕的布局未出现,只有智能手机设备的主屏幕。
我做错了什么?例如,我是否需要在清单中进行某些设置?

我例如遵循了这个教程:
https://www.youtube.com/watch?v=2gljhNFKimk
看起来非常简单。在查看了清单上的一些提示之后,经过一天的努力,我没有取得任何进展。

提前感谢您的帮助,我肯定是错过了一些简单的东西。

英文:

To access a 2nd screen via a button on the 1st screen I followed internet tutorials, so I inserted

  • a 2nd activity and
  • a 2nd layout.

In the 2nd layout I added e.g. a textView and
in the 1st activity I added a button:

package com.example.inko_gnito

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.activity.ComponentActivity

class MainActivity : ComponentActivity() {
private lateinit var startbutton: Button
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_screen)
    startbutton = findViewById(R.id.startbutton)
    startbutton.setOnClickListener {
        val intent = Intent(this, Playerselect::class.java)
        startActivity(intent)
    }
}
}`

When I run the 1st activitx it works as always. When I click the button to access the 2nd screen the layout of the 2nd screen does not appear but only the smart phone device main screen.
What am I doing wrong? Do I have something to do in the manifest for example?

I followed for example this tutorial:
https://www.youtube.com/watch?v=2gljhNFKimk
and it seems to work very simple. AFter checking out some tips on the manifest I don't get any further after 1 day.

Thanks in advance for Your help, it must be something simple I'm missing.

答案1

得分: 1

感谢您的快速回复,我现在已经自己找到了我的错误。清单需要另外两行,这两行在教程中是自动生成的,但不是我创建第二个MainActivity2的方式。清单应该如下所示:

<activity
    android:name=".PlayerSelect"
    android:exported="false"
    android:label="@string/title_activity_player_select"
    android:theme="@style/Theme.Inko_Gnito" />

→ 我漏掉了最后两行。当一个人(像我一样)没有输入一个新的“空活动”,而是输入了其他任何一个活动时,就会发生这种情况(我找不到像教程中显示的“空活动”,但现在找到了)。

英文:

thanks for the quick response, I now found my mistake myself. The manifest needed 2 further lines which were automatically created in the tutorials but not the way I created the 2nd MainActivity2. Manifest must look like this:

    &lt;activity
        android:name=&quot;.PlayerSelect&quot;
        android:exported=&quot;false&quot;
        android:label=&quot;@string/title_activity_player_select&quot;
        android:theme=&quot;@style/Theme.Inko_Gnito&quot; /&gt;

--> I was missing the two last lines. That happens when one (as me) not enters a new "empty activity" but any another one (I could not fine the "empty activity" as it was shown in the tutorials, but now did).

huangapple
  • 本文由 发表于 2023年5月31日 22:52:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76374795.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定