Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将截击响应转换为变量,以便使用意图将值传递给另一个活动?_Android_Variables_Android Volley - Fatal编程技术网

Android 如何将截击响应转换为变量,以便使用意图将值传递给另一个活动?

Android 如何将截击响应转换为变量,以便使用意图将值传递给另一个活动?,android,variables,android-volley,Android,Variables,Android Volley,我试图将我的截击响应分配给一个变量,这样我就可以使用意图将该值传递给另一个活动 我想知道为什么我的matchingcontacts字符串在logcat中显示null。我看到匹配的联系人为空 我在活动顶部声明了匹配联系人: public class VerifyUserPhoneNumber extends AppCompatActivity { String theMatchingContacts; 如果用户已注册应用程序,则在onCreate中: else { get

我试图将我的截击响应分配给一个变量,这样我就可以使用意图将该值传递给另一个活动

我想知道为什么我的
matchingcontacts
字符串在logcat中显示
null
。我看到
匹配的联系人为空

我在活动顶部声明了匹配联系人:

public class VerifyUserPhoneNumber extends AppCompatActivity  {

String theMatchingContacts;
如果用户已注册应用程序,则在
onCreate
中:

else {

         getPhoneContacts();

        // then start the next activity
        Intent myIntent = new Intent(VerifyUserPhoneNumber.this, PopulistoListView.class);
        //we need phoneNoofUser so we can get user_id and corresponding
        //reviews in the next activity
        myIntent.putExtra("keyName", phoneNoofUser);
        myIntent.putExtra("JsonArrayMatchingContacts", theMatchingContacts);
        System.out.println("phonenoofuser" + phoneNoofUser);
        System.out.println("the matching contacts are " + theMatchingContacts);

        VerifyUserPhoneNumber.this.startActivity(myIntent);
我正在看
phoneNoofUser
好的,这很有效。但对于匹配联系人,它会打印
null
。函数
getPhoneContacts()
发生在
部分调用下面的截取代码之前,因此
getMatchingContacts
应该初始化,对吗

我的截击密码是:

StringRequest stringRequest = new StringRequest(Request.Method.POST, CHECKPHONENUMBER_URL,
        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                    System.out.println(response);

                theMatchingContacts = response.toString();

                System.out.println(theMatchingContacts );

                   etc...etc...
StringRequest StringRequest=new StringRequest(Request.Method.POST,CHECKPHONENUMBER\u URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
System.out.println(响应);
MatchingContacts=response.toString();
System.out.println(匹配触点);
等等…等等。。。

响应
打印正确。代码中截取部分的匹配联系人
打印正确。我无法将意向代码放入截取调用中,因为在调用
startActivity
截取在后台线程中异步执行请求之前,我的活动需要做其他事情。因此,执行顺序主线程中的离子将如下所示:

  • 调用了
    getPhoneContacts();
  • Volley在工作线程中启动网络请求
  • 下一个
    活动
    PopulistoListView
    以匹配联系人的
    为空值开始
  • 截击请求完成,并在
    onResponse
    中设置匹配联系人的值

  • 因此,当您启动
    PopulationListView
    活动时,
    MatchingContacts
    的值仍然是
    null
    ,因为截击请求尚未完成。

    Volley在后台线程中异步执行请求。因此,主线程中的执行顺序将是l例如:

  • 调用了
    getPhoneContacts();
  • Volley在工作线程中启动网络请求
  • 下一个
    活动
    PopulistoListView
    以匹配联系人的
    为空值开始
  • 截击请求完成,并在
    onResponse
    中设置匹配联系人的值

  • 因此,当您启动
    populistView
    活动时,MatchingContacts
    的值仍然是
    null
    ,因为截击请求尚未完成。

    您应该在截击请求的
    OnResponse
    回调方法中执行启动新活动的代码,因为s Bob说,截击请求是异步的,您希望在该请求完成后转到下一个活动。

    您应该在截击请求的
    OnResponse
    回调方法中执行启动新活动的代码,因为正如Bob所说,截击请求是异步的,您希望在该请求完成后转到下一个活动。

    如果如果要执行任何与主线程相关的工作,请尝试使用
    runOnUiThread()
    method如果要执行任何与主线程相关的工作,请尝试使用
    runOnUiThread()
    方法