'ARCORE: Sceneform 1.16 issue with rendering

I am using Sceneform 1.16 and andriod studio 4.1, I am trying to render but it is not working.

Issue:

activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment android:name="com.google.ar.sceneform.ux.ArFragment"  <======Error ArFragment must be a fragment >
        android:id="@+id/ux_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</FrameLayout>
  1. MainActivity.java.

    public class MainActivity extends AppCompatActivity {

     private ArFragment arFragment;
     private ModelRenderable modelRenderable;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
    
         arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment);
    

Error:

Inconvertible types; cannot cast 'androidx.fragment.app.Fragment' to 'com.google.ar.sceneform.ux.ArFragment'

we followed the steps in : https://github.com/google-ar/sceneform-android-sdk

--Getting started with Sceneform 1.16.0

Can you please tell what is the issue.

Build gradle file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.examples.myapplication"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions{
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


    api project(":sceneformux")



}


Solution 1:[1]

You should edit the settings.gradle file. Add these lines:

include ':sceneform'
project(':sceneform').projectDir = new File('sceneformsrc/sceneform')

include ':sceneformux'
project(':sceneformux').projectDir = new File('sceneformux/ux')

Solution 2:[2]

Do you have a Fragment class that extends the ArFragment? I had it like

<fragment android:name="com.example.zzz.FaceARFragment"
    android:id="@+id/face_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

It was a class FaceARFragment extends ArFragment used for configuration only. And in activity:

FaceARFragment arFragment = (FaceARFragment) getSupportFragmentManager().findFragmentById(R.id.face_fragment);

That worked for me. Also you can try using Sceneform 1.17, I tried using 1.16, but 1.17 worked better for me as I was using .glb models.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gurkan
Solution 2 CottaLotties