onActivityResult崩溃app kotlin

时间:2018-02-28 01:58:23

标签: android kotlin

我正在创建一个基本的meme生成器,并且在将更改的文本返回到主活动时遇到问题。截至目前,当我运行应用程序时,我可以单击角落中的编辑图标,它将我带到selectionScreen活动。在这里,您可以编辑顶部和底部值的文本。还显示了mainActivity中图像的url。这旨在以允许用户输入他们自己的URL并检索他们想要应用文本的图像的方式运行,但我还没有接近解决该问题。当用户单击保存图标时,它应该返回更改的文本并将其应用于显示的图像。截至目前它只是崩溃了我的应用程序。如果我在onActivityResult中注释掉我的两个值,它允许导航回mainActivity,但显然没有应用任何更改。

代码如下: MainActivity:

package com.example.willo.memegene
import android.app.Activity
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle

import android.support.v7.app.AlertDialog.*
import android.view.Menu
import android.view.MenuItem
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_selection_screen.*

class MainActivity : AppCompatActivity() {

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

        val url = 
"http://i.dailymail.co.uk/i/pix/2016/03/18/15/324D202500000578-3498922-
image-a-33_1458315465874.jpg"

       Picasso.with(this)
                .load(url)
                .into(imageView)
        edit.setOnClickListener{

            val intent = Intent(this, SelectionScreen::class.java).apply {
                putExtra("When nothing works", (topText.text.toString()))
                putExtra("And then it all works", 
(bottomText.text.toString()))
                putExtra("URL", url )
            }

            startActivityForResult(intent,1)
        }






   }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: 
Intent?) {
       super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != 
null){

            val returnedTop = intent.getStringExtra("Back to main")
            topText.setText(returnedTop)

            val returnedBottom = intent.getStringExtra("Off with you")
            bottomText.setText(returnedBottom)



        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.user_options, menu)
        return super.onCreateOptionsMenu(menu)

    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {


        Builder(this)

                .setTitle("About")
                .setMessage(" Name: MemeGene \n Author: Kenneth Willoughby 
\n " +
                        "Version: 1.0")
                .setPositiveButton(android.R.string.ok, null)
                .show()
        return super.onOptionsItemSelected(item)
    }


}

SelectionScreen活动:

package com.example.willo.memegene

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_main.*

import kotlinx.android.synthetic.main.activity_selection_screen.*
import kotlinx.android.synthetic.main.content_selection_screen.*

class SelectionScreen : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_selection_screen)
        setSupportActionBar(toolbar)

       val url = 
"http://i.dailymail.co.uk/i/pix/2016/03/18/15/324D202500000578-3498922-
image-a-33_1458315465874.jpg"

        //Picasso.with(this)
               //.load(url)
             // .into(imageView2)

        val topText = intent.getStringExtra("When nothing works")
            topText2.setText(topText)

        val bottomText = intent.getStringExtra("And then it all works")
            bottomText2.setText(bottomText)

        val link = intent.getStringExtra("URL")
            url2.setText(link)


        save.setOnClickListener{
            val i = Intent(this, MainActivity::class.java).apply{
                putExtra("Back to main", topText2.text.toString())
                putExtra("Off with you", bottomText2.text.toString())
                putExtra("URL Again", url2.text.toString())

            }

            val returnIntent = this.intent
            returnIntent.putExtra("Results",i)
            setResult(Activity.RESULT_OK, returnIntent)
            finish()
        }

        }

    }

**** **** EDIT

我修复了崩溃的问题,但是,现在当用户选择保存小部件时,它会将它们带回MainActivity.kt但没有文本。原始的硬编码文本已经消失,因此我知道它正在返回一些内容以使原始数据消失。我无法弄清楚的是为什么我不能让它传回从SelectionScreen.kt输入的文本

1 个答案:

答案 0 :(得分:0)

您将意图放入另一个意图并期望第一个原始结果。

 val i = Intent(this, MainActivity::class.java).apply{
            putExtra("Back to main", topText2.text.toString())
            putExtra("Off with you", bottomText2.text.toString())
            putExtra("URL Again", url2.text.toString())

        }

更改为

        val returnIntent = this.intent
        returnIntent.putExtra("Results",i)
        setResult(Activity.RESULT_OK, returnIntent)
        finish()

这一个

        setResult(Activity.RESULT_OK, i)
        finish()

这应该会给你想要的结果。