Closed lengleng1992 closed 6 years ago
Haha, I found my very stupid mistake, I forgot to modify the setContentView()
in the new Activity, it should point to my new interface xml!
i have same issue package com.example.umair.dragfinal;
import android.net.Uri; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout;
import com.google.ar.core.Anchor; import com.google.ar.core.Frame; import com.google.ar.core.HitResult; import com.google.ar.core.Plane; import com.google.ar.core.Trackable; import com.google.ar.core.TrackingState; import com.google.ar.sceneform.AnchorNode; import com.google.ar.sceneform.rendering.ModelRenderable; import com.google.ar.sceneform.rendering.Renderable; import com.google.ar.sceneform.ux.ArFragment; import com.google.ar.sceneform.ux.TransformableNode;
import java.util.List; import java.util.concurrent.CompletableFuture;
public class MainActivity extends AppCompatActivity { private ArFragment fragment; private PointerDrawable pointer = new PointerDrawable(); private boolean isTracking; private boolean isHitting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment = (ArFragment)
getSupportFragmentManager().findFragmentById(R.id.sceneform_fragment);
fragment.getArSceneView().getScene().addOnUpdateListener(frameTime -> {
fragment.onUpdate(frameTime);
onUpdate();
});
initializeGallery();
}
private void onUpdate() {
boolean trackingChanged = updateTracking();
View contentView = findViewById(android.R.id.content);
if (trackingChanged) {
if (isTracking) {
contentView.getOverlay().add(pointer);
} else {
contentView.getOverlay().remove(pointer);
}
contentView.invalidate();
}
if (isTracking) {
boolean hitTestChanged = updateHitTest();
if (hitTestChanged) {
pointer.setEnabled(isHitting);
contentView.invalidate();
}
}
}
private boolean updateTracking() {
Frame frame = fragment.getArSceneView().getArFrame();
boolean wasTracking = isTracking;
isTracking = frame != null &&
frame.getCamera().getTrackingState() == TrackingState.TRACKING;
return isTracking != wasTracking;
}
private boolean updateHitTest() {
Frame frame = fragment.getArSceneView().getArFrame();
android.graphics.Point pt = getScreenCenter();
List<HitResult> hits;
boolean wasHitting = isHitting;
isHitting = false;
if (frame != null) {
hits = frame.hitTest(pt.x, pt.y);
for (HitResult hit : hits) {
Trackable trackable = hit.getTrackable();
if (trackable instanceof Plane &&
((Plane) trackable).isPoseInPolygon(hit.getHitPose())) {
isHitting = true;
break;
}
}
}
return wasHitting != isHitting;
}
private android.graphics.Point getScreenCenter() {
View vw = findViewById(android.R.id.content);
return new android.graphics.Point(vw.getWidth()/2, vw.getHeight()/2);
}
private void initializeGallery() {
LinearLayout gallery = findViewById(R.id.gallery_layout);
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.droid_thumb);
andy.setContentDescription("andy");
andy.setOnClickListener(view ->{addObject(Uri.parse("andy.sfb"));});
gallery.addView(andy);
ImageView cabin = new ImageView(this);
cabin.setImageResource(R.drawable.cabin_thumb);
cabin.setContentDescription("cabin");
cabin.setOnClickListener(view ->{addObject(Uri.parse("Cabin.sfb"));});
gallery.addView(cabin);
ImageView house = new ImageView(this);
house.setImageResource(R.drawable.house_thumb);
house.setContentDescription("house");
house.setOnClickListener(view ->{addObject(Uri.parse("House.sfb"));});
gallery.addView(house);
ImageView igloo = new ImageView(this);
igloo.setImageResource(R.drawable.igloo_thumb);
igloo.setContentDescription("igloo");
igloo.setOnClickListener(view ->{addObject(Uri.parse("igloo.sfb"));});
gallery.addView(igloo);
}
//Add the addObject method
private void addObject(Uri model) {
Frame frame = fragment.getArSceneView().getArFrame();
android.graphics.Point pt = getScreenCenter();
List<HitResult> hits;
if (frame != null) {
hits = frame.hitTest(pt.x, pt.y);
for (HitResult hit : hits) {
Trackable trackable = hit.getTrackable();
if (trackable instanceof Plane &&
((Plane) trackable).isPoseInPolygon(hit.getHitPose())) {
placeObject(fragment, hit.createAnchor(), model);
break;
}
}
}
}
//Add placeObject method
private void placeObject(ArFragment fragment, Anchor anchor, Uri model) {
CompletableFuture<Void> renderableFuture =
ModelRenderable.builder()
.setSource(fragment.getContext(), model)
.build()
.thenAccept(renderable -> addNodeToScene(fragment, anchor, renderable))
.exceptionally((throwable -> {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(throwable.getMessage())
.setTitle("Codelab error!");
AlertDialog dialog = builder.create();
dialog.show();
return null;
}));
}
//add obj
private void addNodeToScene(ArFragment fragment, Anchor anchor, Renderable renderable) {
AnchorNode anchorNode = new AnchorNode(anchor);
TransformableNode node = new TransformableNode(fragment.getTransformationSystem());
node.setRenderable(renderable);
node.setParent(anchorNode);
fragment.getArSceneView().getScene().addChild(anchorNode);
node.select();
}
} null pointer execption are Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.ar.sceneform.ArSceneView com.google.ar.sceneform.ux.ArFragment.getArSceneView()' on a null object reference at com.example.umair.dragfinal.MainActivity.onCreate(MainActivity.java:39)
I am trying to add the augmented image feature to my application, based on the SDK example. But when I was clicking the button connected to AugmentedImageActivity, the app will crash. there is my logcat:
I think the issue may be here,but I don't know how to solve it.
arFragment.getArSceneView().getScene().addOnUpdateListener(this::onUpdateFrame);
I am a newbie, please help me solve this problem, thank you very much!!