Fragment$InstantiationException on inflating androidx fragment from android.app.Activity

huangapple go评论62阅读模式
英文:

Fragment$InstantiationException on inflating androidx fragment from android.app.Activity

问题

我遇到了以下异常:

android.app.Fragment$InstantiationException: 尝试实例化一个不是 Fragment 的类 com.example.android.btVspTest.StageCollectionFragment

异常在以下调用中触发:android.app.Activity.setContentView,在我的 VspTestActivity 类的 onCreate() 方法中,该类扩展了 android.app.Activity

setContentView(R.layout.activity_vsp_test);

我的 com.example.android.btVspTest.StageCollectionFragment 扩展了 androidx.fragment.app.Fragment。我猜测问题与 android.app.Activityandroidx.fragment.app.Fragment 之间的不兼容有关。是否有修复或解决方法?

应用级别的 build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 26
        targetSdkVersion 29
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0'
    /*implementation 'com.google.android.material:material:1.1.0-alpha08'*/
    implementation 'androidx.viewpager2:viewpager2:1.0.0'
}

activity_vsp_test.xml:

<LinearLayout 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:orientation="vertical"
    android:id="@+id/test_layout">

    <!-- 其他布局元素已移除 -->

    <fragment class="com.example.android.btVspTest.StageCollectionFragment"
        android:id="@+id/test_stage_collection_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>

StageCollectionFragment.java:

package com.example.android.btVspTest;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.example.android.bluetoothvsp.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class StageCollectionFragment extends Fragment implements TabLayoutMediator.TabConfigurationStrategy {
    StageCollectionAdapter demoCollectionAdapter;
    ViewPager2 viewPager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.test_stage_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        demoCollectionAdapter = new StageCollectionAdapter(this);
        viewPager = view.findViewById(R.id.test_stage_pager);
        viewPager.setAdapter(demoCollectionAdapter);

        TabLayout tabLayout = view.findViewById(R.id.test_stage_tab_layout);
        new TabLayoutMediator(tabLayout, viewPager, this).attach();
    }

    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        tab.setText("OBJECT " + (position + 1));
    }
}

VspTestActivity.java:

package com.example.android.btVspTest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.bluetoothvsp.ActivityMessageDataKey;
import com.example.android.bluetoothvsp.BluetoothActivity;
import com.example.android.bluetoothvsp.DeviceListActivity;
import com.example.android.bluetoothvsp.R;
import com.example.android.bluetoothvsp.enums.ConnectionState;
import com.example.android.bluetoothvsp.enums.TestState;
import com.example.android.common.logger.Log;
import com.example.android.common.logger.LogWrapper;
import com.example.android.common.logger.MessageOnlyLogFilter;

public class VspTestActivity extends Activity {
    private static final String TAG = "VspTestActivity";

    private TextView mConnectionTextView;
    private TextView mTestResultTextView;
    private Button mConnectButton;
    private Button mDisconnectButton;

    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothDevice mDevice;
    private BluetoothVspTestManager mVspTestManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vsp_test);

        if (savedInstanceState != null) {
            return;
        }

        // Irrelevant code removed
    }

    // Irrelevant code removed
    
    @Override
    protected  void onStart() {
        super.onStart();
        // Irrelevant code removed
    }

    // Irrelevant code removed
}
英文:

I'm getting the following exception:

android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.android.btVspTest.StageCollectionFragment that is not a Fragment

The exception triggers within the following call to android.app.Activity.setContentView from onCreate() of my VspTestActivity class, which extends android.app.Activity:
setContentView(R.layout.activity_vsp_test);

My com.example.android.btVspTest.StageCollectionFragment extends androidx.fragment.app.Fragment. I am guessing the issue has to do with incompatibility beween android.app.Activity and androidx.fragment.app.Fragment. Is there a fix or work-around?

Application level build.gradle:

apply plugin: &#39;com.android.application&#39;
android {
compileSdkVersion 29
defaultConfig {
minSdkVersion 26
targetSdkVersion 29
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
implementation &#39;androidx.appcompat:appcompat:1.1.0&#39;
implementation &#39;androidx.constraintlayout:constraintlayout:1.1.3&#39;
implementation &#39;com.google.android.material:material:1.1.0&#39;
/*implementation &#39;com.google.android.material:material:1.1.0-alpha08&#39;*/
implementation &#39;androidx.viewpager2:viewpager2:1.0.0&#39;
}

activity_vsp_test.xml:

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:orientation=&quot;vertical&quot;
android:id=&quot;@+id/test_layout&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;@dimen/margin_huge&quot;
android:orientation=&quot;horizontal&quot;&gt;
&lt;Button
android:id=&quot;@+id/test_connect_button&quot;
android:layout_width=&quot;126dp&quot;
android:layout_height=&quot;match_parent&quot;
android:text=&quot;@string/connect&quot; /&gt;
&lt;Button
android:id=&quot;@+id/test_disconnect_button&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_weight=&quot;1&quot;
android:text=&quot;@string/disconnect&quot; /&gt;
&lt;Button
android:id=&quot;@+id/test_bluetooth_button&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_weight=&quot;1&quot;
android:onClick=&quot;startBluetoothActivity&quot;
android:text=&quot;Bluetooth&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;TextView
android:id=&quot;@+id/vsp_test_connection_text&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/disconnected&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/test_result_text&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot; /&gt;
&lt;!-- 2 irrelevant linear layouts with buttons in them removed --&gt;
&lt;fragment class=&quot;com.example.android.btVspTest.StageCollectionFragment&quot;
android:id=&quot;@+id/test_stage_collection_fragment&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:layout_weight=&quot;1&quot;/&gt;
&lt;/LinearLayout&gt;

StageCollectionFragment.java:

package com.example.android.btVspTest;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;
import com.example.android.bluetoothvsp.R;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.android.material.tabs.TabLayoutMediator.TabConfigurationStrategy;
public class StageCollectionFragment extends Fragment implements TabConfigurationStrategy {
// When requested, this adapter returns a StageFragment,
// representing an object in the collection.
StageCollectionAdapter demoCollectionAdapter;
ViewPager2 viewPager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_stage_layout, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
demoCollectionAdapter = new StageCollectionAdapter(this);
viewPager = view.findViewById(R.id.test_stage_pager);
viewPager.setAdapter(demoCollectionAdapter);
TabLayout tabLayout = view.findViewById(R.id.test_stage_tab_layout);
new TabLayoutMediator(tabLayout, viewPager, this).attach();
}
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
tab.setText(&quot;OBJECT &quot; + (position + 1));
}
}

VspTestActivity.java:

package com.example.android.btVspTest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.android.bluetoothvsp.ActivityMessageDataKey;
import com.example.android.bluetoothvsp.BluetoothActivity;
import com.example.android.bluetoothvsp.DeviceListActivity;
import com.example.android.bluetoothvsp.R;
import com.example.android.bluetoothvsp.enums.ConnectionState;
import com.example.android.bluetoothvsp.enums.TestState;
import com.example.android.common.logger.Log;
import com.example.android.common.logger.LogWrapper;
import com.example.android.common.logger.MessageOnlyLogFilter;
public class VspTestActivity extends Activity
{
/**
* Debugging.
*/
private static final String TAG = &quot;VspTestActivity&quot;;
/**
* UI elements.
*/
private TextView mConnectionTextView;
private TextView mTestResultTextView;
private Button mConnectButton;
private Button mDisconnectButton;
private Button mSynchronousTestButton;
private Button mHostSpamButton;
private Button mReaderSpamButton;
private Button mBothSpamButton;
/**
* Objects provided by Android to facilitate Bluetooth operations.
*/
private BluetoothAdapter mBluetoothAdapter;
private BluetoothDevice mDevice;
/**
* Manger used to start and handle the communication tests.
*/
private BluetoothVspTestManager mVspTestManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vsp_test);
if (savedInstanceState != null) {
return;
}
// Irrelevant code removed
}
// Irrelevant code removed
@Override
protected  void onStart() {
super.onStart();
// Irrelevant code removed
}
// Irrelevant code removed
}

Some of the code is copy->paste->edit of example at https://developer.android.com/guide/navigation/navigation-swipe-view-2, with following mapping:

  • My StageCollectionFragment is their CollectionDemoFragment
  • My StageFragment is their DemoObjectFragment

That developer.android.com example unfortunately isn't complete as the layout resources are missing. I haven't been able to find a full example on the internet, of combining TabLayout & ViewPager2.

I'm trying to do something like the draft screen at <https://imgur.com/a/k8Tr52y?>. I would eventually have 4 to 5 tabs with actual meaningful tab titles, with the tabs being added dynamically, and specific fragments in each of the tabs.

答案1

得分: 1

你的活动必须扩展 androidx.fragment.app.FragmentActivity

文档中可以看到:

> 这是用于希望使用基于支持库的 Fragment 的活动的基类。

此外,根据Fragment 的文档

> - 你的活动必须扩展 FragmentActivity
>
>
>
> - 你必须调用 FragmentActivity.getSupportFragmentManager() 来获取 FragmentManager

英文:

Your activity must extend androidx.fragment.app.FragmentActivity.

From the documentation,

> Base class for activities that want to use the support-based
> Fragments.

Also from Fragment's documentation,

> - Your activity must extend FragmentActivity
>
>
>
> - You must call FragmentActivity.getSupportFragmentManager() to get the FragmentManager

答案2

得分: 0

由于您的代码的一部分正在使用AndroidX库,我将假设您愿意全面采用并且仅使用AndroidX。您应该这样做。目前,您正在混合使用旧版和新版的Android库,这并不是一件好事。只需在声明时执行以下操作(注意导入语句):

您的活动(Activity):

import androidx.appcompat.app.AppCompatActivity;

public class VspTestActivity extends AppCompatActivity {

以及您的片段(Fragment):

import androidx.fragment.app.Fragment;

public class StageCollectionFragment extends Fragment ...

甚至更简单的方法是让Android Studio为您完成所有操作。只需选择“重构” > “迁移到AndroidX”(在Mac上)。

英文:

Since part of your code is using AndroidX libraries, I'll assume you are okay with going all-in and use AndroidX exclusively. You should. Right now, you are mixing old and new Android libraries and that's not a good thing. Simply do the following when declaring (pay attention to the import statements):

your activity:

import androidx.appcompat.app.AppCompatActivity;

public class VspTestActivity extends AppCompatActivity {

and your fragment:

import androidx.fragment.app.Fragment

public class StageCollectionFragment extends Fragment ...

Even easier is to let Android Studio do it all for you. Just select Refactor > Migrate to AndroidX (on the Mac)

huangapple
  • 本文由 发表于 2020年5月30日 06:23:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/62095429.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定