简体   繁体   中英

Why is my Android Studio (Kotlin) showing the layout of an Android phone in Preview but not in the Build App?

I made a simple app in the Android Studio. I took two TextViews and one Button in XML layout file. Both textview and Button are showing in the preview but in the Build Apk there is only Button Showing. It doesn't giving any error. I don't know what is the problem I tried all the possible ways to fix it but didn't get anything.

activity_home XML code

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

    <EditText android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:hint="mnail"
              android:id="@+id/editText"/>

    <EditText
            android:id="@+id/textViewSendEmail"
            android:layout_width="344dp"
            android:layout_height="44dp"
            android:hint="Email"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"/>
    <EditText
            android:id="@+id/textViewSendPassword"
            android:layout_width="344dp"
            android:layout_height="44dp"
            android:hint="Password"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:layout_below="@+id/textViewSendEmail"/>

    <Button
            android:id="@+id/sendButton"
            android:layout_width="344dp"
            android:layout_height="44dp"
            android:text="send"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:layout_below="@+id/textViewSendPassword"/>

</LinearLayout>

MainActivity code

package com.kotlinintent

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_home.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        var email_user = textViewSendEmail.text
        var password_user = textViewSendPassword.text

        sendButton.setOnClickListener {
            intent = Intent(this, SecondActvity::class.java)
            intent.putExtra("email", email_user)
            intent.putExtra("password", password_user)
            startActivity(intent)
        }
    }
}

SecondActivity

package com.kotlinintent

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_second.*

class SecondActvity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)


        var bundle : Bundle? = intent.extras
        var emailData = bundle!!.getString("email")
        var passwordData = bundle.getString("password")

        textViewEmail.text = emailData
        textViewPassword.text = passwordData
    }
}

Second Activity XML

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

    <TextView
            android:id="@+id/textViewEmail"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:text="Email"/>
    <TextView
            android:id="@+id/textViewPassword"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:text="Password"
            android:layout_marginTop="40dp"
            />

    <Button
            android:id="@+id/getButton"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:text="get"
            android:layout_marginTop="40dp"
            />
</LinearLayout>

Instead of these lines

var email_user = textViewSendEmail.text
var password_user = textViewSendPassword.text

Use this

var email_user = textViewSendEmail.text.toString()
var password_user = textViewSendPassword.text.toString()

And also you must call these lines in onclick so your code must like this

 sendButton.setOnClickListener {
        var email_user = textViewSendEmail.text.toString()
        var password_user = textViewSendPassword.text.toString()
        intent = Intent(this, SecondActvity::class.java)
        intent.putExtra("email", email_user)
        intent.putExtra("password", password_user)
        startActivity(intent)
    }

And i suggest that use wrap_content instead of static size ( 44dp ) for height

Your issue in the way how you put extras. In MainActivity you put your data as extras for intent. But in SecondActivity you try to get data from intent's bundle.

Change this

var bundle : Bundle? = intent.extras
var emailData = bundle!!.getString("email")
var passwordData = bundle.getString("password")

for this

var emailData = intent.getStringExtra("email")
var passwordData = intent.getStringExtra("password")`

Or you can put data into Bundle in MainActivity, then put this Bundle into intent. And then get data, as you do with your current solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM