Fragment$InstantiationException 从 android.app.Activity 膨胀 androidx 片段
Fragment$InstantiationException on inflating androidx fragment from android.app.Activity
我遇到以下异常:
android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.android.btVspTest.StageCollectionFragment that is not a Fragment
在我的 VspTestActivity
class 的 onCreate()
对 android.app.Activity.setContentView
的以下调用中触发了异常,它扩展了 android.app.Activity
:
setContentView(R.layout.activity_vsp_test);
我的 com.example.android.btVspTest.StageCollectionFragment
扩展了 androidx.fragment.app.Fragment
。我猜这个问题与 android.app.Activity
和 androidx.fragment.app.Fragment
之间的不兼容有关。是否有修复或 work-around?
应用级别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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/margin_huge"
android:orientation="horizontal">
<Button
android:id="@+id/test_connect_button"
android:layout_width="126dp"
android:layout_height="match_parent"
android:text="@string/connect" />
<Button
android:id="@+id/test_disconnect_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/disconnect" />
<Button
android:id="@+id/test_bluetooth_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="startBluetoothActivity"
android:text="Bluetooth" />
</LinearLayout>
<TextView
android:id="@+id/vsp_test_connection_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/disconnected" />
<TextView
android:id="@+id/test_result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 2 irrelevant linear layouts with buttons in them removed -->
<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;
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("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
{
/**
* Debugging.
*/
private static final String TAG = "VspTestActivity";
/**
* 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
}
一些代码是在 https://developer.android.com/guide/navigation/navigation-swipe-view-2 处复制->粘贴->编辑示例,具有以下映射:
- 我的 StageCollectionFragment 是他们的 CollectionDemoFragment
- 我的 StageFragment 是他们的 DemoObjectFragment
不幸的是,developer.android.com 示例并不完整,因为缺少布局资源。我无法在互联网上找到结合 TabLayout 和 ViewPager2 的完整示例。
我正在尝试做一些类似于 https://imgur.com/a/k8Tr52y? 的草稿屏幕。我最终会有 4 到 5 个带有实际有意义的标签标题的标签,标签是动态添加的,每个标签中都有特定的片段。
您的 activity 必须扩展 androidx.fragment.app.FragmentActivity
。
Base class for activities that want to use the support-based
Fragments.
Your activity must extend FragmentActivity
You must call FragmentActivity.getSupportFragmentManager() to get the FragmentManager
由于您的部分代码正在使用 AndroidX 库,因此我假设您可以全力以赴并只使用 AndroidX。你应该。现在,您正在混合新旧 Android 库,这不是一件好事。声明时只需执行以下操作(注意导入语句):
你的activity:
import androidx.appcompat.app.AppCompatActivity;
public class VspTestActivity extends AppCompatActivity {
和你的片段:
import androidx.fragment.app.Fragment
public class StageCollectionFragment extends Fragment ...
更简单的方法是让 Android Studio 为您代劳。只需 select 重构 > 迁移到 AndroidX(在 Mac 上)
我遇到以下异常:
android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.android.btVspTest.StageCollectionFragment that is not a Fragment
在我的 VspTestActivity
class 的 onCreate()
对 android.app.Activity.setContentView
的以下调用中触发了异常,它扩展了 android.app.Activity
:
setContentView(R.layout.activity_vsp_test);
我的 com.example.android.btVspTest.StageCollectionFragment
扩展了 androidx.fragment.app.Fragment
。我猜这个问题与 android.app.Activity
和 androidx.fragment.app.Fragment
之间的不兼容有关。是否有修复或 work-around?
应用级别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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/margin_huge"
android:orientation="horizontal">
<Button
android:id="@+id/test_connect_button"
android:layout_width="126dp"
android:layout_height="match_parent"
android:text="@string/connect" />
<Button
android:id="@+id/test_disconnect_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/disconnect" />
<Button
android:id="@+id/test_bluetooth_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="startBluetoothActivity"
android:text="Bluetooth" />
</LinearLayout>
<TextView
android:id="@+id/vsp_test_connection_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/disconnected" />
<TextView
android:id="@+id/test_result_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 2 irrelevant linear layouts with buttons in them removed -->
<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;
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("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
{
/**
* Debugging.
*/
private static final String TAG = "VspTestActivity";
/**
* 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
}
一些代码是在 https://developer.android.com/guide/navigation/navigation-swipe-view-2 处复制->粘贴->编辑示例,具有以下映射:
- 我的 StageCollectionFragment 是他们的 CollectionDemoFragment
- 我的 StageFragment 是他们的 DemoObjectFragment
不幸的是,developer.android.com 示例并不完整,因为缺少布局资源。我无法在互联网上找到结合 TabLayout 和 ViewPager2 的完整示例。
我正在尝试做一些类似于 https://imgur.com/a/k8Tr52y? 的草稿屏幕。我最终会有 4 到 5 个带有实际有意义的标签标题的标签,标签是动态添加的,每个标签中都有特定的片段。
您的 activity 必须扩展 androidx.fragment.app.FragmentActivity
。
Base class for activities that want to use the support-based Fragments.
Your activity must extend FragmentActivity
You must call FragmentActivity.getSupportFragmentManager() to get the FragmentManager
由于您的部分代码正在使用 AndroidX 库,因此我假设您可以全力以赴并只使用 AndroidX。你应该。现在,您正在混合新旧 Android 库,这不是一件好事。声明时只需执行以下操作(注意导入语句):
你的activity:
import androidx.appcompat.app.AppCompatActivity;
public class VspTestActivity extends AppCompatActivity {
和你的片段:
import androidx.fragment.app.Fragment
public class StageCollectionFragment extends Fragment ...
更简单的方法是让 Android Studio 为您代劳。只需 select 重构 > 迁移到 AndroidX(在 Mac 上)