我正在通过位置开始结果方法的活动。 place是一个字符串,声明为String place。
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
结果的内部启动活动我尝试使用这两种方法检索其值,但它们都没有工作。
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
答案 0 :(得分:0)
您可以在global variable
中声明一个activity
并指定一些您希望传递给onActivityResult()
的特定值,如果您在全局范围内声明了这个值,则可以轻松访问该值:
String PLACE;
...
....
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
PLACE = place;
//intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
在这种情况下,每次点击按钮时PLACE的值都会有所不同:
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);
}
});
}
}
答案 1 :(得分:0)
您正试图获取final String place = data.getStringExtra("location");
,但数据中不包含名称为&#39; location&#39;没有人设定它。
仅在您打开图库时才设置它,而不是相反!
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.putExtra("location",place);
startActivityForResult(intent, RESULT_TRUE);
顺便说一句,您已经拥有位置值是活动本身为什么不使用它!
答案 2 :(得分:0)
看来你还没有正确理解这个概念!这可能有助于您理解 startActivityForResult 。在此解释之后,请看下面的答案!
使用startActivityForResult(Intent intent, int requestCode)
,您可以开始另一个活动,然后在onActivityResult()
方法中从该活动接收结果。{{1来自你开始另一个Activity的地方。
示例强>
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.
}
}
}
}
&lt; - 这是我们用来向AnotherActivity
发送数据的那个
MainActivity
对于你的任务,你不需要以上的方式来获取你的知识,如下所示
public class AnotherActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
final View button = findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new Intent as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the AnotherActivity to
// return to MainActivity
finish();
}
});
}
@Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// to Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
按你想要的按钮!并且它们指向一个名为 <Button
android:onClick="gatherResults" <--- method name in your class
android:id="@+id/button_id_one"
android:layout_width="match_parent"
android:layout_height="100dp" />
的方法,如上所述,然后过滤使用其ID点击的方法
现在在你的班级中写下这个方法,
gatherResults
答案 3 :(得分:0)
您将值放在要启动的activity
的意图中。因此,您只能在该新活动中获得此值。如果你想在当前活动中使用这个值,那么只需维护一个全局变量,点击你的按钮就可以相应地更新它的值。
答案 4 :(得分:0)
试试这个: -
片段: -
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 {
}
}
}
希望这会对你有所帮助:-): - )