android.app.Fragment$InstantiationException: 尝试实例化一个 class 不是片段的 ScannerFragment
android.app.Fragment$InstantiationException: Trying to instantiate a class ScannerFragment that is not a Fragment
我更新了 app.gradle 中的一些库,例如 22.2.0 -> 23.0.0 的支持库和一些第三方库的版本,现在我在一个特定的 activity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shubhamkanodia.bookmybook/com.example.shubhamkanodia.bookmybook.AddBooksActivity_}: android.view.InflateException: Binary XML file line #155: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.view.InflateException: Binary XML file line #155: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:606)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2110)
at android.app.Activity.onCreateView(Activity.java:5348)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:606)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2110)
at android.app.Activity.onCreateView(Activity.java:5348)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
特别是这一行看起来很奇怪:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment that is not a Fragment
(ScannerFragment 是实现 dm77 条形码扫描仪的片段,我从 1.7.2 更新到 1.8.2)
扫描仪碎片:
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import me.dm7.barcodescanner.zbar.BarcodeFormat;
import me.dm7.barcodescanner.zbar.Result;
import me.dm7.barcodescanner.zbar.ZBarScannerView;
public class ScannerFragment extends Fragment implements MessageDialogFragment.MessageDialogListener,
ZBarScannerView.ResultHandler, FormatSelectorDialogFragment.FormatSelectorDialogListener,
CameraSelectorDialogFragment.CameraSelectorDialogListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
mScannerView = new ZBarScannerView(getActivity());
//stuff
return mScannerView;
}
@Override
public void onActivityCreated(Bundle state){
super.onCreate(state);
//stuff
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
//stuff
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
//stuff
}
@Override
public void onResume() {
super.onResume();
//stuff
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//stuff
}
@Override
public void handleResult(Result rawResult) {
//stuff
}
@Override
public void onPause() {
super.onPause();
//stuff
}
XML 片段:
<FrameLayout
android:id="@+id/flScan"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fScanner"
class="com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="8dp"
android:background="@color/transparent"
android:padding="12dp">
<ImageButton
android:id="@+id/ibAutofocus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ripple_light"
android:clickable="true"
android:contentDescription="Enable / Disable Autofocus"
android:longClickable="true"
android:padding="8dp"
android:src="@mipmap/ic_action_auto_focus_on" />
<ImageButton
android:id="@+id/ibFlashControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ibAutofocus"
android:background="@drawable/ripple_light"
android:clickable="true"
android:contentDescription="Enable / Disable Flash"
android:longClickable="true"
android:padding="8dp"
android:src="@mipmap/ic_action_image_flash_off" />
</RelativeLayout>
</FrameLayout>
如有任何帮助,我们将不胜感激!
尝试导入 android.app.Fragment
而不是 android.support.v4.app.Fragment
如果不需要,基本上不要使用支持库 ' 或检查 Activity 你在其中膨胀这个片段: 是否仅使用支持库中的 FragmentManager
和 FragmentTransaction
。
我更新了 app.gradle 中的一些库,例如 22.2.0 -> 23.0.0 的支持库和一些第三方库的版本,现在我在一个特定的 activity:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shubhamkanodia.bookmybook/com.example.shubhamkanodia.bookmybook.AddBooksActivity_}: android.view.InflateException: Binary XML file line #155: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.view.InflateException: Binary XML file line #155: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:606)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2110)
at android.app.Activity.onCreateView(Activity.java:5348)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.ClassCastException
at android.app.Fragment.instantiate(Fragment.java:606)
at android.app.Fragment.instantiate(Fragment.java:582)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2110)
at android.app.Activity.onCreateView(Activity.java:5348)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:257)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.setContentView(AddBooksActivity_.java:56)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity.onCreate(AddBooksActivity.java:140)
at com.example.shubhamkanodia.bookmybook.AddBooksActivity_.onCreate(AddBooksActivity_.java:46)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access0(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
特别是这一行看起来很奇怪:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment that is not a Fragment
(ScannerFragment 是实现 dm77 条形码扫描仪的片段,我从 1.7.2 更新到 1.8.2)
扫描仪碎片:
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import me.dm7.barcodescanner.zbar.BarcodeFormat;
import me.dm7.barcodescanner.zbar.Result;
import me.dm7.barcodescanner.zbar.ZBarScannerView;
public class ScannerFragment extends Fragment implements MessageDialogFragment.MessageDialogListener,
ZBarScannerView.ResultHandler, FormatSelectorDialogFragment.FormatSelectorDialogListener,
CameraSelectorDialogFragment.CameraSelectorDialogListener {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
mScannerView = new ZBarScannerView(getActivity());
//stuff
return mScannerView;
}
@Override
public void onActivityCreated(Bundle state){
super.onCreate(state);
//stuff
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
//stuff
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
//stuff
}
@Override
public void onResume() {
super.onResume();
//stuff
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//stuff
}
@Override
public void handleResult(Result rawResult) {
//stuff
}
@Override
public void onPause() {
super.onPause();
//stuff
}
XML 片段:
<FrameLayout
android:id="@+id/flScan"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fScanner"
class="com.example.shubhamkanodia.bookmybook.Fragments.ScannerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginTop="8dp"
android:background="@color/transparent"
android:padding="12dp">
<ImageButton
android:id="@+id/ibAutofocus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ripple_light"
android:clickable="true"
android:contentDescription="Enable / Disable Autofocus"
android:longClickable="true"
android:padding="8dp"
android:src="@mipmap/ic_action_auto_focus_on" />
<ImageButton
android:id="@+id/ibFlashControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ibAutofocus"
android:background="@drawable/ripple_light"
android:clickable="true"
android:contentDescription="Enable / Disable Flash"
android:longClickable="true"
android:padding="8dp"
android:src="@mipmap/ic_action_image_flash_off" />
</RelativeLayout>
</FrameLayout>
如有任何帮助,我们将不胜感激!
尝试导入 android.app.Fragment
而不是 android.support.v4.app.Fragment
如果不需要,基本上不要使用支持库 ' 或检查 Activity 你在其中膨胀这个片段: 是否仅使用支持库中的 FragmentManager
和 FragmentTransaction
。