google-ar / sceneform-android-sdk

Sceneform SDK for Android
https://developers.google.com/sceneform/develop/
Apache License 2.0
1.23k stars 604 forks source link

Application crashed when clicking the button connected to augmented image interface #235

Closed lengleng1992 closed 6 years ago

lengleng1992 commented 6 years ago

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:

08/07 00:33:08: Launching app
$ adb shell am start -n "com.ly1992.ly.arcoredemo/com.ly1992.ly.arcoredemo.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: com.ly1992.ly.arcoredemo.test | com.ly1992.ly.arcoredemo
Waiting for application to come online: com.ly1992.ly.arcoredemo.test | com.ly1992.ly.arcoredemo
Waiting for application to come online: com.ly1992.ly.arcoredemo.test | com.ly1992.ly.arcoredemo
Connecting to com.ly1992.ly.arcoredemo
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
W/ActivityThread: Application com.ly1992.ly.arcoredemo is waiting for the debugger on port 8100...
I/System.out: Sending WAIT chunk
I/zygote64: Debugger is active
I/System.out: Debugger has connected
              waiting for debugger to settle...
Connected to the target VM, address: 'localhost:8600', transport: 'socket'
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: waiting for debugger to settle...
I/System.out: debugger has settled (1339)
I/InstantRun: starting instant run server: is main process
D/OpenGLRenderer: HWUI GL Pipeline
D/ViewRootImpl@c5f455b[MainActivity]: setView = DecorView@3e46d1[MainActivity] TM=true MM=false
V/StudioProfiler: StudioProfilers agent attached.
V/StudioProfiler: Acquiring Application for Events
V/StudioProfiler: Transformed class: java/net/URL
W/zygote64: Current dex file has more than one class in it. Calling RetransformClasses on this class might fail if no transformations are applied to it!
V/StudioProfiler: Memory control stream started.
W/Activity: AppLock checkAppLockState locked:false verifying:false pkgName = com.ly1992.ly.arcoredemo isInMultiWindowMode:false
D/ViewRootImpl@c5f455b[MainActivity]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@c5f455b[MainActivity]: Relayout returned: old=[0,0][0,0] new=[0,0][1080,2220] result=0x7 surface={valid=true 513002868736} changed=true
I/Adreno: QUALCOMM build                   : bafb8dc, Ic43eb7c732
          Build Date                       : 02/01/18
          OpenGL ES Shader Compiler Version: EV031.22.00.02
          Local Branch                     : 
          Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.UM.6.3.C1.08.00.00.432.017
          Remote Branch                    : NONE
          Reconstruct Branch               : NOTHING
I/Adreno: PFP: 0x016ee155, ME: 0x00000000
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 2
D/libGLESv1: STS_GLApi : DTS, ODTC are not allowed for Package : com.ly1992.ly.arcoredemo
D/OpenGLRenderer: eglCreateWindowSurface = 0x7767359750
D/ViewRootImpl@c5f455b[MainActivity]: MSG_RESIZED_REPORT: frame=Rect(0, 0 - 1080, 2220) ci=Rect(0, 63 - 0, 126) vi=Rect(0, 63 - 0, 126) or=1
D/ViewRootImpl@c5f455b[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@264e4e2 nm : com.ly1992.ly.arcoredemo ic=null
I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@b7e8073 nm : com.ly1992.ly.arcoredemo ic=null
V/StudioProfiler: Live memory tracking disabled.
V/StudioProfiler: Live memory tracking enabled.
                  JNIEnv not attached
V/StudioProfiler: Loaded classes: 5311
V/StudioProfiler: Tracking initialization took: 246248854ns
D/ViewRootImpl@c5f455b[MainActivity]: ViewPostIme pointer 0
D/ViewRootImpl@c5f455b[MainActivity]: ViewPostIme pointer 1
D/ViewRootImpl@c5f455b[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 0
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.ly1992.ly.arcoredemo, PID: 17250
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ly1992.ly.arcoredemo/com.ly1992.ly.arcoredemo.augmented_image.AugmentedImageActivity}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6938)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
                   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.ly1992.ly.arcoredemo.augmented_image.AugmentedImageActivity.onCreate(AugmentedImageActivity.java:41)
                      at android.app.Activity.performCreate(Activity.java:7174)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
                      at android.os.Handler.dispatchMessage(Handler.java:105) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6938) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
I/zygote64: Do partial code cache collection, code=26KB, data=29KB
I/zygote64: After code cache collection, code=26KB, data=29KB
            Increasing code cache capacity to 128KB
I/zygote64: Do partial code cache collection, code=26KB, data=53KB
I/zygote64: After code cache collection, code=26KB, data=53KB
            Increasing code cache capacity to 256KB
            Compiler allocated 9MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'

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!!

lengleng1992 commented 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!

umairabbasi12 commented 6 years ago

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)