Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.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/2/tensorflow/5.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
Java 在活动之间传递字符串_Java_Android_String_Android Intent - Fatal编程技术网

Java 在活动之间传递字符串

Java 在活动之间传递字符串,java,android,string,android-intent,Java,Android,String,Android Intent,大家好 当我想将ContactView中的字符串输入MainActivity时,我遇到了问题 我试图在ContactView中创建一个意图,并在MainActivity中从此意图中检索数据 我遇到的问题是,当我在ContactView中执行任何操作时,它会打开MainActivity: 我所做的是: ContactView.java //this is where the Activity gets opend right? Intent getContact = new

大家好

当我想将ContactView中的字符串输入MainActivity时,我遇到了问题

我试图在ContactView中创建一个意图,并在MainActivity中从此意图中检索数据

我遇到的问题是,当我在ContactView中执行任何操作时,它会打开MainActivity:

我所做的是:

ContactView.java

     //this is where the Activity gets opend right?
      Intent getContact = new Intent(this, MainActivity.class); 
      getContact.putExtra("Contact",phoneNo);
      startActivity(getContact);
这就是我检索它所做的:

 Bundle extras = getIntent().getExtras();
 if (extras != null) {
     //this is where i should get the contact?
     String readContact= extras.getString("Contact")
 }
我在ContactView中有一个ContactPick方法,它可以获取contactname和phonenumber,这实际上就是我想要传递给我的主要活动的内容,因为我有一个按钮,可以通过ContactView中的文本和联系人发送SMS

这是我的方法:

      private void contactPicked(Intent data) {
        Cursor cursor = null;
        try {
            String phoneNo = null;
            String name = null;

            // getData() method will have the Content Uri of the selected contact
            Uri uri = data.getData();

            //Query the content uri
            cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();
            // column index of the phone number
            int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            // column index of the contact name
            int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            phoneNo = cursor.getString(phoneIndex);
            name = cursor.getString(nameIndex);
            // Set the value to the textviews
            textView1.setText(name);
            textView2.setText(phoneNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
我是否需要将意图放入我的方法中?即使在我选择联系人后,它仍将为我打开活动

我是不是错过了我找不到的东西

有人能帮我吗

###编辑

这是我点击按钮选择联系人的地方:

public void onClick(View v) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
###编辑2

我的活动的顺序是MainActivity(MapView),即ContactView(我在其中选择联系人并显示他们)

我仍然想选择联系人并在我的ContactView中显示它们,但当我再次在我的MainActivity中时,我有一个按钮,可以从ContactView中获取字符串并对其执行一些操作,如发送短信

ContactView.java

public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85;
private TextView textView1;
private TextView textView2;
private TextView message;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
    textView1 = (TextView) findViewById(R.id.TxtName);
    textView2 = (TextView) findViewById(R.id.TxtNumber);
    message = (TextView) findViewById(R.id.message);

    //speicher den aktuellen status der activity
    SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
    String name = settings.getString("contactName", "");
    //the second parameter set a default data if “contactName” is empty
    if (!name.isEmpty()){
        textView1.setText(name);
    }
    String phoneNo = settings.getString("contactPhone", "");//the second parameter set a default data if “contactName” is empty
    if (!phoneNo.isEmpty()){
        textView2.setText(phoneNo);
    }
}

public void onClick(View v) {
    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // check whether the result is ok
    if (resultCode == RESULT_OK) {
        // Check for the request code, we might be usign multiple startActivityForReslut
        switch (requestCode) {
            case RESULT_PICK_CONTACT:
                contactPicked(data);
                break;
        }
    } else {
        Log.e("ContactView", "Failed to pick contact");
    }
}

/**
 * Query the Uri and read contact details. Handle the picked contact data.
 *
 * @param data
 */
private void contactPicked(Intent data) {
    Cursor cursor = null;
    try {
        String phoneNo = null;
        String name = null;
        String msg=message.getText().toString();
        //String ResqMessage = null;
        // getData() method will have the Content Uri of the selected contact
        Uri uri = data.getData();

        //Query the content uri
        cursor = getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        // column index of the phone number
        int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        // column index of the contact name
        int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        phoneNo = cursor.getString(phoneIndex);
        name = cursor.getString(nameIndex);
        // Set the value to the textviews
        textView1.setText(name);
        textView2.setText(phoneNo);
        SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("contactName", name);
        editor.putString("contactPhone", phoneNo);
        editor.commit();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

您是否在主要活动中使用startActivityForResult

通常,在第一个活动中调用startActivityForResult(..),然后在onActivityResult方法(在主活动中)中处理结果。 为此,您必须通过setResult(intent i)在第二个活动中设置结果


你在这么做吗?如果没有,请搜索它,否则我可以给您一些代码。

实际上您收到的方式是错误的。查看您的代码,您的
MainActivity
没有传递任何捆绑包,因此在获得传递的联系人之前不需要调用
getExtras()
。您可以从
MainActivity
执行以下操作:

//start main
Intent getContact = new Intent(this, MainActivity.class); 
getContact.putExtra("Contact",phoneNo);
startActivity(getContact);
// In MainActivity onCreate
String contact = getIntent().getStringExtra("Contact");

您应该使用此代码进行解析

  Intent intent = new Intent(this,FirstActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("data","value");
    intent.putExtras(bundle);
    startActivity(intent);
要在SecondActivity中获取数据,请使用以下代码:

Bundle bundle = getIntent().
bundle.getString("data");

您能否指定活动顺序以及它们之间的通信?MainActivity是第一个?您确定在将其设置为textview时获得了联系人姓名和号码吗?是的,我得到了他们,我只是将他们设置在那里,以便在我的视图中显示。请发布ContactView类代码,但您将此意图称为:intent getContact=new intent(此,MainActivity.class);getContact.putExtra(“联系人”,电话号码);星触觉(getContact);??不,我在主活动中没有startActivityForResult或onActivityResult,仅在我的联系人视图中加载联系人时,您不想出于某种原因使用它还是不知道?如果您想使用它,下面是一个如何使用它的示例:我不需要在ContactView中使用它吗?:Intent getContact=newintent(this,MainActivity.class);getContact.putExtra(“联系人”,电话号码);星触觉(getContact);在我的主要活动中,使用onCreate:String contact=intent.getStringExtra(“contact”);我的主要活动中没有电话,只有ContactView中没有。您的
ContactView
是什么?正如我在您的代码中看到的,
联系人视图
不是您的,它是系统联系人视图对吗(Intent.ACTION\u PICK)?如果是,如何在contactView中调用start
MainActivity
?添加了contactView。。。事实上这是一门课是的,我看到了。因此,如果您像上面那样启动
main活动
,并在
onCreate
中获取电话号码,代码将正常工作。这里没有必要使用
SharePreference
!那么告诉我你在哪里评论开始主。。我是否将此写在我的主要活动中?我怎样才能拿到一个字符串呢??我要为这个创建一个空字符串吗?然后我在create上做了其他的工作??