0

Currently, I am developing an android app using PhotoEditor library. I have followed the Getting Started and Setting up the View sections as explained on the page. However, somehow, I cannot inflate the UI XML that contains PhotoEditorView. Here is my code:

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

        <ja.burhanrashid52.photoeditor.PhotoEditorView
            android:id="@+id/photoEditorView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ja.burhanrashid52.photoeditor.PhotoEditorView>

</RelativeLayout>

Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    try {
        final View view = inflater.inflate(R.layout.app_fragment_edit_picture, container, false);
    }
    catch (Exception e) {
        LogHelper.LogError("TEST");
    }

    return null;
}

I put the try-catch and return null there only for testing.

Error:

Binary XML file line #9: Binary XML file line #9: Error inflating class ja.burhanrashid52.photoeditor.PhotoEditorView

Does anyone have a clue?

As requested, here is the full stack trace:

android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class ja.burhanrashid52.photoeditor.PhotoEditorView at android.view.LayoutInflater.inflate(LayoutInflater.java:539) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at com.mobileop.app.gui.fragments.EditPictureFragment.onCreateView(EditPictureFragment.java:54) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229) at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2466) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1483) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5551) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class ja.burhanrashid52.photoeditor.PhotoEditorView at android.view.LayoutInflater.createView(LayoutInflater.java:645) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) ... 30 more Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at android.view.LayoutInflater.createView(LayoutInflater.java:619) ... 35 more Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lja/burhanrashid52/photoeditor/R$styleable; at ja.burhanrashid52.photoeditor.PhotoEditorView.init(PhotoEditorView.java:72) at ja.burhanrashid52.photoeditor.PhotoEditorView.(PhotoEditorView.java:48) ... 37 more Caused by: java.lang.ClassNotFoundException: Didn't find class "ja.burhanrashid52.photoeditor.R$styleable" on path: DexPathList[[zip file "/data/app/com.mobileop-1/base.apk"],nativeLibraryDirectories=[/data/app/com.mobileop-1/lib/arm, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) ... 39 more Suppressed: java.lang.ClassNotFoundException: ja.burhanrashid52.photoeditor.R$styleable at java.lang.Class.classForName(Native Method) at java.lang.BootClassLoader.findClass(ClassLoader.java:781) at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) at java.lang.ClassLoader.loadClass(ClassLoader.java:504) ... 40 more Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Yusril Maulidan Raji
  • 1,682
  • 1
  • 21
  • 46

2 Answers2

1

You have to return a View, you are returning null in the function.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
savedInstanceState) {
// Inflate the layout for this fragment
try {
    final View view = inflater.inflate(R.layout.app_fragment_edit_picture, container, 
false);
}
catch (Exception e) {
    LogHelper.LogError("TEST");
}

return view;
}
Adnan Temur
  • 333
  • 1
  • 10
  • No, I put it there only for debugging. As I mentioned in the question: `I put the try-catch and return null there only for testing.`. It always goes to `catch`. – Yusril Maulidan Raji Oct 09 '19 at 14:40
  • @YusrilMaulidanRaji Well then you should return view in try block and null in the catch block. Returning the view is necessary as the function's return type is view. public "View" onCreateView(....) – Adnan Temur Oct 09 '19 at 14:43
  • Yes, I did it like so. But still, it triggers the exception. Therefore, I don't think that's the source of the problem – Yusril Maulidan Raji Oct 09 '19 at 15:06
  • @YusrilMaulidanRaji Is your layout file error free. How did you import this library "ja.burhanrashid52.photoeditor.PhotoEditorView". Did you add any dependency in the gradle file. – Adnan Temur Oct 09 '19 at 16:01
  • Yes, I can compile and run it on my phone. I have also set it in gradle: `implementation files('libs/ja.burhanrashid52-photoeditor-0.4.0.jar')`. – Yusril Maulidan Raji Oct 09 '19 at 16:04
  • I have followed the steps to generate the .jar file here: https://stackoverflow.com/a/52681317/3097810 . I also followed the steps here https://stackoverflow.com/a/35241990/3097810 to add the .jar file. – Yusril Maulidan Raji Oct 09 '19 at 16:04
  • Also, when I press Ctrl + B on ` – Yusril Maulidan Raji Oct 09 '19 at 16:10
  • @YusrilMaulidanRaji Yes, it means the dependency is imported. I have tested your code with fragments and added an updated answer. It works fine. – Adnan Temur Oct 09 '19 at 17:13
1

Update

Whole Code with Main,fragment xmls and classes.

MainActivity XML

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

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragmentContainer"/>

</LinearLayout>

MainActivity Java

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class PhotoEditorActivity extends AppCompatActivity {
FragmentManager fragManager;
FragmentTransaction fragTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo_editor);

    fragManager=getFragmentManager();
    fragManager.findFragmentById(R.id.fragmentContainer);
    fragTransaction=fragManager.beginTransaction();
    fragTransaction.replace(R.id.fragmentContainer, new PhotoEditorFragment());
    fragTransaction.commit();

}
}

Fragment XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">

<ja.burhanrashid52.photoeditor.PhotoEditorView
    android:id="@+id/photoEditorView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ja.burhanrashid52.photoeditor.PhotoEditorView>

</RelativeLayout>

Fragment Java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PhotoEditorFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle 
savedInstanceState) {
    View itemView=inflater.inflate(R.layout.photo_editor_fragment,container,false);
    try
    {
        return itemView;}
    catch (Exception e){
        return null;
    }

}
}

This code runs fine on my android studio. I have not imported the jar but added the dependency in gradle

dependencies {
    implementation 'ja.burhanrashid52:photoeditor:0.4.0'
}
Adnan Temur
  • 333
  • 1
  • 10
  • Have you tried running it on a device? In my case, android studio doesn’t show any error. Only when I run it on a device, then it shows the mentioned error. – Yusril Maulidan Raji Oct 09 '19 at 17:18
  • @YusrilMaulidanRaji Yes I have and it didn't show me an error. Run the above code, I am sure it will do. – Adnan Temur Oct 09 '19 at 17:25
  • Hi, sorry for the late reply. Okay then. I'll retry by making a small project. Hopefully, I can point out the real source of the problem – Yusril Maulidan Raji Oct 10 '19 at 07:12
  • It works now, thanks! The keyword is `I have not imported the jar but added the dependency in gradle`. Previously, I did that as well, but there was an error in `AndroidManifest.xml`. I thought I had to import the jar first, but I shouldn't. – Yusril Maulidan Raji Oct 10 '19 at 07:29