gpt4 book ai didi

java - 如何从一个 Activity 接收数据到另一个 Activity - android

转载 作者:行者123 更新时间:2023-12-02 01:41:56 25 4
gpt4 key购买 nike

我正在尝试将一个整数从 Android Studio 中的一个 Activity 发送到另一个 Activity 。在我的源类中,我使用 putExtra() 发送数据,在接收者类中,我尝试使用 getIntent() 接收数据。但是,我收到错误“无法解析接收者类中的方法‘getIntent()’”。这是我的代码:

我是 Android studio 和 Java 的新手,所以如果我遗漏了一些非常明显的东西,请体谅。

来源 Activity :

public class AugmentedImageActivity extends AppCompatActivity {

private ArFragment arFragment;
private ImageView fitToScanView;

// Augmented image and its associated center pose anchor, keyed by the augmented image in
// the database.
private final Map<AugmentedImage, AugmentedImageNode> augmentedImageMap = new HashMap<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
fitToScanView = findViewById(R.id.image_view_fit_to_scan);

arFragment.getArSceneView().getScene().addOnUpdateListener(this::onUpdateFrame);
}

@Override
protected void onResume() {
super.onResume();
if (augmentedImageMap.isEmpty()) {
fitToScanView.setVisibility(View.VISIBLE);
}
}

/**
* Registered with the Sceneform Scene object, this method is called at the start of each frame.
*
* @param frameTime - time since last frame.
*/
private void onUpdateFrame(FrameTime frameTime) {
Frame frame = arFragment.getArSceneView().getArFrame();

// If there is no frame or ARCore is not tracking yet, just return.
if (frame == null || frame.getCamera().getTrackingState() != TrackingState.TRACKING) {
return;
}

Collection<AugmentedImage> updatedAugmentedImages =
frame.getUpdatedTrackables(AugmentedImage.class);
for (AugmentedImage augmentedImage : updatedAugmentedImages) {
switch (augmentedImage.getTrackingState()) {
case PAUSED:
// When an image is in PAUSED state, but the camera is not PAUSED, it has been detected,
// but not yet tracked.
int value=augmentedImage.getIndex();
Intent i = new Intent(AugmentedImageActivity.this, AugmentedImageNode.class);
i.putExtra("key",value);
startActivity(i);
String text = "Detected Image " + augmentedImage.getIndex();
SnackbarHelper.getInstance().showMessage(this, text);
break;

case TRACKING:
// Have to switch to UI Thread to update View.
fitToScanView.setVisibility(View.GONE);

// Create a new anchor for newly found images.
if (!augmentedImageMap.containsKey(augmentedImage)) {
AugmentedImageNode node = new AugmentedImageNode(this);
node.setImage(augmentedImage);
augmentedImageMap.put(augmentedImage, node);
arFragment.getArSceneView().getScene().addChild(node);
}
break;

case STOPPED:
augmentedImageMap.remove(augmentedImage);
break;
}
}
}
}

收件人 Activity :

public class AugmentedImageNode extends AnchorNode {

private static final String TAG = "AugmentedImageNode";

// The augmented image represented by this node.
private AugmentedImage image;

private static CompletableFuture<ModelRenderable> ulCorner;

public AugmentedImageNode(Context context) {

Intent intent = getIntent();
Bundle extras = intent.getExtras();
int value = extras.getInt("key");

if (value == 0) {
// Upon construction, start loading the models for the corners of the frame.
if (ulCorner == null) {
ulCorner =
ModelRenderable.builder()
.setSource(context, Uri.parse("models/tinker.sfb"))
//.setSource(context, Uri.parse("models/borderfence-small2.sfb"))
.build();
}
}
}
@SuppressWarnings({"AndroidApiChecker", "FutureReturnValueIgnored"})
public void setImage(AugmentedImage image) {
this.image = image;

// If any of the models are not loaded, then recurse when all are loaded.
if (!ulCorner.isDone())// || !urCorner.isDone() || !llCorner.isDone() || !lrCorner.isDone())
{
CompletableFuture.allOf(ulCorner)//, urCorner, llCorner, lrCorner)
.thenAccept((Void aVoid) -> setImage(image))
.exceptionally(
throwable -> {
Log.e(TAG, "Exception loading", throwable);
return null;
});
}

// Set the anchor based on the center of the image.
setAnchor(image.createAnchor(image.getCenterPose()));

// Make the 4 corner nodes.
Vector3 localPosition = new Vector3();
Node cornerNode;
localPosition.set(-0.0f * image.getExtentX(), 0.1f, +0.5f * image.getExtentZ());
cornerNode = new Node();
cornerNode.setParent(this);
cornerNode.setLocalPosition(localPosition);
cornerNode.setLocalRotation(Quaternion.axisAngle(new Vector3(-1f, 0, 0), 90f));
cornerNode.setRenderable(ulCorner.getNow(null));
}

private void setLocalRotation() {
}

public AugmentedImage getImage() {
return image;
}
}

最佳答案

getIntent() 方法仅在扩展 activity[直接或间接]的类中可用

关于java - 如何从一个 Activity 接收数据到另一个 Activity - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54350435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com