gpt4 book ai didi

java - void androidx.constraintlayout.widget.ConstraintLayout.setVisibility(int) - 在空对象引用上

转载 作者:行者123 更新时间:2023-12-02 01:02:50 26 4
gpt4 key购买 nike

我遇到一个错误,ConstraintLayout 中存在空对象引用

Logcat

E/UncaughtException: java.lang.NullPointerException: Attempt to invokevirtual method 'voidandroidx.constraintlayout.widget.ConstraintLayout.setVisibility(int)'on a null object referenceat com.dreamKatcher.Core.Profile.ProfileFragment.onCreateView(ProfileFragment.java:136)at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2595)at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:434)at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1866)at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1821)at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2660)at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2610)at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1413)at android.app.Activity.performStart(Activity.java:7169)at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3146)at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:193)at android.app.ActivityThread.main(ActivityThread.java:6923)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)

public class ProfileFragment extends AbstractBaseFragment implements ProfileView {

@BindView(R.id.appBarMain)
AppBarLayout appBar;

@BindView(R.id.appBar)
ConstraintLayout app_bar;

@BindView(R.id.nameTextView)
TextView nameTextView;

@BindView(R.id.profileDescTextView)
AppCompatTextView profileDescTextView;

@BindView(R.id.detailsViewPager)
CustomViewPager detailsViewPager;

@BindView(R.id.detailsTabLayout)
TabLayout detailsTabLayout;

@BindView(R.id.nest)
NestedScrollView nest;

@BindView(R.id.profileImageView)
CircularImageView profileImageView;

@BindView(R.id.collapsingToolbar)
CollapsingToolbarLayout collapsingToolbar;

Unbinder unbinder;

@BindView(R.id.settings)
ImageView settings;

@BindView(R.id.backButton)
ImageView backButton;

@BindView(R.id.title)
TextView title;

@BindView(R.id.search)
ImageView search;

@BindView(R.id.leader_board)
ImageView leaderBoard;

@BindView(R.id.typeandnickname)
TextView typeandnickname;

@BindView(R.id.img_prize)
ImageView img_prize;

@BindView(R.id.img_games)
ImageView img_games;

ProfilePresenter profilePresenter;

public static String username;

private ProfileViewPagerAdapter viewPagerAdapter;

public ProfileFragment() {

}

public static ProfileFragment newInstance() {
return new ProfileFragment();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_user_profile, container, false);

unbinder = ButterKnife.bind(this, view);
app_bar.setVisibility(View.GONE);
settings.setVisibility(View.VISIBLE);
settings.setOnClickListener(this);
img_games.setOnClickListener(this);
img_prize.setOnClickListener(this);
return view;

}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
nest.setFillViewport(true);


}

private void setUpAppbarLayout() {

appBar.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
if (Math.abs(verticalOffset) == appBarLayout.getTotalScrollRange()) {
} else {

}
});

}

}

之前一段时间它工作得很好,我没有改变这个类中的任何内容。如何解决这个问题?任何建议都会有帮助。

编辑 1现在问题改为TutorialActivity

public class TutorialActivity extends AppCompatActivity {

@BindView(R.id.tutorial_viewpager)
ViewPager tutorialViewpager;
@BindView(R.id.pageindicator)
PageIndicatorView pageindicator;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
ButterKnife.bind(this);
TutorialAdapter tutorialAdapter = new TutorialAdapter(getSupportFragmentManager());
tutorialViewpager.setAdapter(tutorialAdapter);

tutorialViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}
}
}

Logcat

E/UncaughtException: java.lang.RuntimeException: Unable to startactivityComponentInfo{com.dreamKatcher/com.dreamKatcher.Core.Walkthrough.TutorialActivity}:java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)'on a null object referenceat android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3261)at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977)at android.os.Handler.dispatchMessage(Handler.java:106)at android.os.Looper.loop(Looper.java:193)at android.app.ActivityThread.main(ActivityThread.java:6923)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)'on a null object referenceat com.dreamKatcher.Core.Walkthrough.TutorialActivity.onCreate(TutorialActivity.java:34)at android.app.Activity.performCreate(Activity.java:7148)at android.app.Activity.performCreate(Activity.java:7139)at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1293)at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)

编辑 2 我现在可以通过删除 ButterKnife 并使用 findViewById 添加布局默认值来查看 TutoriaActivity我使用的是 ButterKnife 版本 10.1.0,当我升级到最新版本时,问题并未解决。

编辑3Activity_tutorial.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:attrs="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Core.Walkthrough.TutorialActivity">

<androidx.viewpager.widget.ViewPager
android:id="@+id/tutorial_viewpager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<com.rd.PageIndicatorView
android:id="@+id/pageindicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:piv_dynamicCount="true"
app:piv_interactiveAnimation="true"
app:piv_selectedColor="#33FFFFFF"
app:piv_unselectedColor="@color/white"
app:piv_viewPager="@id/tutorial_viewpager"
attrs:piv_padding="1.5dp"
attrs:piv_radius="4dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

您是否通过将以下代码行从 onCreateView() 方法更改为 onActivityCreated() 进行检查

app_bar.setVisibility(View.GONE);
settings.setVisibility(View.VISIBLE);
settings.setOnClickListener(this);
img_games.setOnClickListener(this);
img_prize.setOnClickListener(this);

关于java - void androidx.constraintlayout.widget.ConstraintLayout.setVisibility(int) - 在空对象引用上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60447442/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com