从MainActivity2在MainActivity中的Intent抛出Null值

时间:2018-09-20 03:24:12

标签: android kotlin

主要活动2和主要活动代码。如果我将变量从MainActivity2传递给MainActivity,那么我在Kotlin中将得到null错误。

主要活动2代码

class MainActivity2:AppCompatActivity()  {
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        GetJsonWithOkHttpClient(this.text).execute();
        val test=GetJsonWithOkHttpClient(this.text).execute().get()
        System.out.println("ABC Value is $test")
        val intent = Intent(this@MainActivity2,MainActivity::class.java)
        intent.putExtra("Test",test)
        var jsonStringArray=intent.getStringExtra("Test")
        startActivity(intent)
        System.out.println("TestValue: $jsonStringArray")
    }

主要活动代码

class MainActivity : AppCompatActivity(){
        var jsonStringArray=intent.getStringExtra("Test")
    }

2 个答案:

答案 0 :(得分:0)

删除

Private Function VerifyPassword(pword As String, uname As String) As Boolean
    Using cn As New SqlConnection(My.Settings.UsersConnectionString)
    Dim cmd As New SqlCommand("Select Count(*) From Users Where UserName = @UserName And UserPassword = @Password;", cn)
    cmd.Parameters.Add("@UserName", SqlDbType.VarChar, 100).Value = uname
    cmd.Parameters.Add("@Password", SqlDbType.VarChar, 100).Value = pword

        Try
            cn.Open()
            Dim i As Integer = CInt(cmd.ExecuteScalar())
            If i > 0 Then Return True
            Return False
        Catch ex As Exception
            Throw
        Finally
            cn.Close()
            cmd.Dispose()
        End Try
    End Using
End Function

来自MainActivity2

var jsonStringArray=intent.getStringExtra("Test")

您的主要活动应该是

 class MainActivity2: AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle ? ) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
         GetJsonWithOkHttpClient(this.text).execute();
         val test = GetJsonWithOkHttpClient(this.text).execute().get()
         System.out.println("ABC Value is $test")
         val intent = Intent(this @MainActivity2, MainActivity::class.java)
         intent.putExtra("Test", test)
         startActivity(intent)
         System.out.println("TestValue: $jsonStringArray")
     }

答案 1 :(得分:0)

感谢Sasikumar,我能够使用下面的代码解决此问题。谢谢大家的帮助。 val test = MainActivity2.GetJsonWithOkHttpClient(this.text).execute()。get()。我在主要活动和主要活动2之间更改了文本ID。