Android 无法在StartActivityForResult方法内输入字符串值
我正在传递位置以开始结果方法的活动。place是一个字符串,声明为stringplaceAndroid 无法在StartActivityForResult方法内输入字符串值,android,android-intent,Android,Android Intent,我正在传递位置以开始结果方法的活动。place是一个字符串,声明为stringplace Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); intent.putExtra("location",place); startActivityForResult(intent, RESULT_TRUE); 在result的start activity中,我尝试使用这两种方法检索它的值,但都不起作用 fi
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
在result的start activity中,我尝试使用这两种方法检索它的值,但都不起作用
final String place = data.getStringExtra("location");
final String place = data.getExtras().getString("location");
这就是这个方法的样子
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_TRUE && resultCode == RESULT_OK) {
Uri url = data.getData();
final String place = data.getStringExtra("location");
StorageReference filepath = mstorage.child("photos").child(url.getLastPathSegment());
filepath.putFile(url).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@O
activityresult上的公共void(int-requestCode、int-resultCode、Intent-data){
super.onActivityResult(请求代码、结果代码、数据);
if(requestCode==RESULT\u TRUE&&resultCode==RESULT\u OK){
Uri url=data.getData();
最终字符串位置=data.getStringExtra(“位置”);
StorageReference文件路径=mstorage.child(“照片”).child(url.getLastPathSegment());
filepath.putFile(url).addOnSuccessListener(新的OnSuccessListener(){
@O
您可以在该活动中声明一个全局变量
,并指定要传递给onActivityResult()的特定值。
如果您全局声明该值,则可以轻松访问该值,如下所示:
String PLACE;
...
....
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
PLACE = place;
//intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
在这种情况下,每次单击按钮的位置值将不同:
public class MyActivity extends AppCompatActivity {
String PLACE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xx);
Btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
PLACE = place;
//intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
}
});
Btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
PLACE = place;
//intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
}
});
Btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
PLACE = place;
//intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
}
});
}
}您正在尝试获取最终字符串place=data.getStringExtra(“位置”);
但数据不包含名为“location”的键,因为没有人设置它
它只在你打开画廊时设置,而不是反过来设置
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
顺便说一句,您已经知道位置值是活动本身,为什么不使用它!您似乎还没有正确理解该概念!这可能有助于您理解startActivityForResult。然后在解释后查看下面的答案
通过使用startActivityForResult(Intent-Intent,int-requestCode)
您可以启动另一个活动,然后在onActivityResult()
方法中从该活动接收结果。因此onActivityResult()
是从您启动另一个活动的地方开始的
示例
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9988;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent to start AnotherActivity
final Intent intent = new Intent(this, AnotherActivity.class);
// Start AnotherActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
//-------- When a result is returned from another Activity onActivityResult is called.--------- //
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the AnotherActivity
// By convention RESULT_OK means that what ever
// AnotherActivity did was successful
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// AnotherActivity was not successful. No data to retrieve.
}
}
}
}
AnotherActivity
您正在将该值放入要启动的活动的意图中。因此,您只能在该新活动中获取该值。如果您希望在当前活动中获取该值,则只需维护一个全局变量,然后单击按钮即可相应地更新其值。尝试以下操作:-
片段:-
View v;
public static String Place;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.yourLayout, container, false);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Initialize your buttons
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Place=yourPlace;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, your_Request_code);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Place=yourPlace;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, your_Request_code);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Place=yourPlace;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, your_Request_code);
}
});
}
在活动中:-
String str_place;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_EXAMPLE) {
if(resultCode == Activity.RESULT_OK) {
str_place=yourFragment.Place;
//Do your stuff here and use this place object where you want
} else {
}
}
}
希望这对您有所帮助:-):-)放置是对象还是字符串?放置是字符串在ActivityResult中获取的意图数据是不同的当您在同一活动中有位置时,为什么要传递该位置。通过使用startActivityForResult(intent-intent,int-requestCode)您可以启动另一个活动,然后在onActivityResult()中接收该活动的结果方法。您需要它还是需要将数据发送到另一个活动?buttonA有位置1 buttonB有位置2 buttonC有位置3我想为每个按钮的结果调用活动,并且必须传递位置值这不起作用,因为每个按钮的位置值不同buttonA的位置值是image1,place VabuttonB的lue是image2。我从这些按钮调用onActvitystartMethod,因此我想根据单击的按钮向onActivity方法发送不同的位置值。我从您的代码中得到的是,您试图从应用程序中打开gallery,并使用“位置”键将数据传递给它对吗?选择图像时,将设置结果(图像位置和其他所需数据)调用OnActivityResult方法将在同一活动中调用!哦,我明白了,谢谢。我将删除onActivity开始方法..明天将尝试此方法并更新您。太晚了..谢谢&很好night@user7327850如果您正在使用片段,请在其父级的活动类中使用此gatherResults
,而不是在片段中
public void gatherResults(View view){
// Perform action on click
switch(view.getId()) {
case R.id.button_id_one:
//DO what you want for specific id button_id_one
break;
case R.id.button_id_two:
//DO what you want specific id button_id_two
break;
}
}
View v;
public static String Place;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
v = inflater.inflate(R.layout.yourLayout, container, false);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//Initialize your buttons
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Place=yourPlace;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, your_Request_code);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Place=yourPlace;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, your_Request_code);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Place=yourPlace;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, your_Request_code);
}
});
}
String str_place;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE_EXAMPLE) {
if(resultCode == Activity.RESULT_OK) {
str_place=yourFragment.Place;
//Do your stuff here and use this place object where you want
} else {
}
}
}