- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我点击一个项目时,有时它会工作,有时它会崩溃。
以下是单击项目后的致命异常。
2020-03-31 21:59:18.087 15383-15383/com.aliton.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aliton.myapp, PID: 15383
java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{b3bb13b V.E...C.. .......D 0,336-720,518} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.java:84)
at com.aliton.myapp.Adapter.StoreSelectAdapter$StoreSelectViewHolder$1.onChanged(StoreSelectAdapter.java:147)
at com.aliton.myapp.Adapter.StoreSelectAdapter$StoreSelectViewHolder$1.onChanged(StoreSelectAdapter.java:142)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6351)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:896)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
StoreEntity
),听它的
id
然后导航到具有
direction
中定义的对象的其他 fragment .
public class StoreSelectAdapter extends RecyclerView.Adapter<StoreSelectAdapter.StoreSelectViewHolder> {
private static final String TAG = "debinf SummaryAdapter";
private FragmentActivity fragmentActivity;
public StoreSelectAdapter(FragmentActivity fragmentActivity) {
this.fragmentActivity = fragmentActivity;
}
private static final DiffUtil.ItemCallback<StoreModel> DIFF_CALLBACK = new DiffUtil.ItemCallback<StoreModel>() {
@Override
public boolean areItemsTheSame(@NonNull StoreModel oldItem, @NonNull StoreModel newItem) {
return oldItem.getKey().equals(newItem.getKey());
}
@Override
public boolean areContentsTheSame(@NonNull StoreModel oldItem, @NonNull StoreModel newItem) {
return oldItem.getKey().equals(newItem.getKey()) && oldItem.getName().equals(newItem.getName()) && oldItem.getAddress().equals(newItem.getAddress());
}
};
private AsyncListDiffer<StoreModel> differ = new AsyncListDiffer<StoreModel>(this, DIFF_CALLBACK);
@NonNull
@Override
public StoreSelectViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_store, parent, false);
return new StoreSelectViewHolder(view, fragmentActivity);
}
@Override
public void onBindViewHolder(@NonNull StoreSelectViewHolder holder, int position) {
StoreModel store = differ.getCurrentList().get(position);
Log.i(TAG, "onBindViewHolder: "+store.getName());
holder.storeName.setText(store.getName());
holder.storeAddress.setText(store.getAddress());
if (store.getImage() != null && !store.getImage().isEmpty()) {
Picasso.get().load(store.getImage()).networkPolicy(NetworkPolicy.OFFLINE).into(holder.storeImage, new Callback() {
@Override
public void onSuccess() {
}
@Override
public void onError(Exception e) {
Picasso.get().load(store.getImage()).into(holder.storeImage);
}
});
} else {
holder.storeImage.setImageResource(android.R.drawable.stat_sys_phone_call_on_hold);
}
/*holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "onClick: onBindViewHolder");
Toast.makeText(context, "onClick: onBindViewHolder", Toast.LENGTH_SHORT).show();
AddstoreFragmentDirections.ActionAddstoreFragmentToBarcodeFragment direction = AddstoreFragmentDirections.actionAddstoreFragmentToBarcodeFragment(store);
Navigation.createNavigateOnClickListener()
}
});*/
}
@Override
public int getItemCount() {
return differ.getCurrentList().size();
}
public void submitList(List<StoreModel> stores) {
differ.submitList(stores);
}
public List<StoreModel> getCurrentItemList() {
return differ.getCurrentList();
}
public class StoreSelectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
//private static final String TAG = "StoreSelectViewHolder";
public ImageView storeImage;
public TextView storeName, storeAddress;
private FragmentActivity fragmentActivity;
private LocalDatabaseViewModel localDatabaseViewModel;
public StoreSelectViewHolder(@NonNull View itemView, FragmentActivity fragmentActivity) {
super(itemView);
storeImage = (ImageView) itemView.findViewById(R.id.item_store_image);
storeName = (TextView) itemView.findViewById(R.id.item_store_name);
storeAddress = (TextView) itemView.findViewById(R.id.item_store_address);
this.fragmentActivity = fragmentActivity;
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
StoreModel store = differ.getCurrentList().get(getAdapterPosition());
Log.i(TAG, "onClick: storeName selected: "+store.getName());
Log.i(TAG, "onClick: storeKey selected: "+store.getKey());
StoreEntity storeEntity = new StoreEntity(store.getName(), store.getImage());
storeEntity.setKey(store.getKey());
localDatabaseViewModel = ViewModelProviders.of(fragmentActivity).get(LocalDatabaseViewModel.class);
localDatabaseViewModel.getStoreIdFromInsertedItem().observe(fragmentActivity, new Observer<Long>() {
@Override
public void onChanged(Long itemId) {
Log.i(TAG, "onChanged: itemId in adapter is "+itemId);
SelectstoreFragmentDirections.ActionSelectstoreFragmentToBarcodeFragment direction = SelectstoreFragmentDirections.actionSelectstoreFragmentToBarcodeFragment(store, itemId);
Navigation.findNavController(v).navigate(direction); // this is the StoreSelectAdapter.java:147
}
});
localDatabaseViewModel.insertStore(storeEntity);
}
}
}
activity_main.xml
我的 fragment NavHost 被声明的地方。
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/main_drawer"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/main_fragment"
android:layout_width="0dp"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/main_bottomnav"
app:defaultNavHost="true"
app:navGraph="@navigation/mainnav_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/main_bottomnav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/main_navmenu"
android:background="@color/colorAccent"
app:itemIconTint="@drawable/botton_item_color"
app:itemTextColor="@drawable/botton_item_color">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/main_sidebar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/main_sidebarmenu"/>
</androidx.drawerlayout.widget.DrawerLayout>
mainnav_graph.xml
...
<fragment
android:id="@+id/barcodeFragment"
android:name="com.aliton.myapp.BarcodeFragment"
android:label="fragment_barcode"
tools:layout="@layout/fragment_barcode" >
<argument
android:name="store"
app:argType="com.aliton.myapp.Model.StoreModel" />
<action
android:id="@+id/action_barcodeFragment_to_productdetailFragment"
app:destination="@id/productdetailFragment" />
<argument
android:name="storeId"
app:argType="long" />
</fragment>
<fragment
android:id="@+id/selectstoreFragment"
android:name="com.aliton.myapp.SelectstoreFragment"
android:label="fragment_selectstore"
tools:layout="@layout/fragment_selectstore" >
<action
android:id="@+id/action_selectstoreFragment_to_addstoreFragment"
app:destination="@id/addstoreFragment" />
<action
android:id="@+id/action_selectstoreFragment_to_barcodeFragment"
app:destination="@id/barcodeFragment" />
</fragment>
...
fragment_selectstore.xml
作为
SelectstoreFragment.java
的内容.
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SelectstoreFragment">
<androidx.cardview.widget.CardView
android:id="@+id/selectstore_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
app:cardCornerRadius="10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="@+id/selectstore_question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Are you at any of the store shown below?"
android:textSize="28sp"
android:gravity="center"
android:textStyle="bold"/>
</androidx.cardview.widget.CardView>
<FrameLayout
android:id="@+id/selectstore_framelayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/selectstore_cardview">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/selectstore_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="@+id/selectstore_nodata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="No store found!"
android:textSize="22sp" />
</FrameLayout>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="@+id/selectstore_fab"
style="@style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="8dp"
android:contentDescription="Outra loja desc"
android:text="Outra loja"
app:icon="@drawable/common_full_open_on_phone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
interface
用于
Adapter
之间的通信和
Fragment
获取
view
在
onViewCreated()
,但应用程序不断崩溃。
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
// https://stackoverflow.com/a/53902696/4300670
// NOT WORKING
adapter = new StoreSelectAdapter(getActivity(), new StoreSelectAdapter.OnStoreSelectListener() {
@Override
public void onStoreSelected(StoreModel storeModel, Long itemId) {
SelectstoreFragmentDirections.ActionSelectstoreFragmentToBarcodeFragment direction = SelectstoreFragmentDirections.actionSelectstoreFragmentToBarcodeFragment(storeModel, itemId);
Navigation.findNavController(getActivity(), R.id.main_fragment).navigate(direction);
}
});
storeRecyclerview.setAdapter(adapter);
}
2020-04-02 17:11:22.163 6015-6015/com.aliton.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aliton.myapp, PID: 6015
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.requireViewById(int)' on a null object reference
at androidx.core.app.ActivityCompat.requireViewById(ActivityCompat.java:363)
at androidx.navigation.Navigation.findNavController(Navigation.java:58)
at com.aliton.myapp.SelectstoreFragment$4.onStoreSelected(SelectstoreFragment.java:221)
at com.aliton.myapp.Adapter.StoreSelectAdapter$StoreSelectViewHolder$1.onChanged(StoreSelectAdapter.java:149)
at com.aliton.myapp.Adapter.StoreSelectAdapter$StoreSelectViewHolder$1.onChanged(StoreSelectAdapter.java:145)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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:858)
Navigation
里面的方法类以了解导致错误的原因,它们是:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
adapter = new StoreSelectAdapter(getActivity(), new StoreSelectAdapter.OnStoreSelectListener() {
@Override
public void onStoreSelected(StoreModel storeModel, Long itemId) {
SelectstoreFragmentDirections.ActionSelectstoreFragmentToBarcodeFragment direction = SelectstoreFragmentDirections.actionSelectstoreFragmentToBarcodeFragment(storeModel, itemId);
NavController navController = myFindViewNavController(view);
if (navController == null) {
Log.i(TAG, "onStoreSelected: ISSUE NavController set");
}
}
});
storeRecyclerview.setAdapter(adapter);
}
private NavController myFindViewNavController(View view) {
while (view != null) {
Log.i(TAG, "myFindViewNavController: view != null - "+view);
NavController controller = myGetViewNavController(view);
if (controller != null) {
Log.i(TAG, "myFindViewNavController: controller != null");
return controller;
}
ViewParent parent = view.getParent();
view = parent instanceof View ? (View) parent : null;
Log.i(TAG, "myFindViewNavController: view.getParent() - "+view);
}
return null;
}
private NavController myGetViewNavController(View view) {
Object tag = view.getTag(R.id.nav_controller_view_tag);
Log.i(TAG, "myGetViewNavController: tag is "+tag);
NavController controller = null;
if (tag instanceof WeakReference) {
Log.i(TAG, "myGetViewNavController: tag instanceof WeakReference");
controller = ((WeakReference<NavController>) tag).get();
} else if (tag instanceof NavController) {
Log.i(TAG, "myGetViewNavController: tag instanceof NavController");
controller = (NavController) tag;
}
return controller;
}
ViewModel
在我的
adapter
导航到下一个 fragment 后再次触发:
debinf SummaryAdapter: onClick: storeName selected: Posto Shell Convem
debinf SummaryAdapter: onClick: storeKey selected: M0Ysk5jRIdtiJ9zyUhXG
debinf SummaryAdapter: onChanged: itemId in adapter is 40, view id: -1
debinf SelStoreFrag: myFindViewNavController: view != null - androidx.constraintlayout.widget.ConstraintLayout{fb9835d V.E...... ........ 0,0-1080,1437}
debinf SelStoreFrag: myGetViewNavController: tag is null
debinf SelStoreFrag: myFindViewNavController: view.getParent() - android.widget.FrameLayout{4af5206 V.E...... ........ 0,0-1080,1437 #7f090137 app:id/main_fragment}
debinf SelStoreFrag: myFindViewNavController: view != null - android.widget.FrameLayout{4af5206 V.E...... ........ 0,0-1080,1437 #7f090137 app:id/main_fragment}
debinf SelStoreFrag: myGetViewNavController: tag is androidx.navigation.NavController@47f0c7
debinf SelStoreFrag: myGetViewNavController: tag instanceof NavController
debinf SelStoreFrag: myFindViewNavController: controller != null
debinf BarcodeFrag: onCreateView:
debinf BarcodeFrag: onCreateView: fragArgs is com.aliton.myapp.Model.StoreModel@77fbfcb
debinf BarcodeFrag: onPermissionGranted:
debinf BarcodeFrag: startCamera:
debinf ExtFun: isFlashSupported: false
debinf ExtFun: startCameraForAllDevices:
debinf BarcodeFrag: onResume:
debinf SummaryAdapter: onChanged: itemId in adapter is 41, view id: -1
debinf SelStoreFrag: myFindViewNavController: view != null - androidx.constraintlayout.widget.ConstraintLayout{fb9835d V.E...... .......D 0,0-1080,1437}
debinf SelStoreFrag: myGetViewNavController: tag is null
debinf SelStoreFrag: myFindViewNavController: view.getParent() - null
debinf SelStoreFrag: onStoreSelected: ISSUE NavController set
id
通过
ViewModel
在
adapter
.
最佳答案
我的 上有一些类似的问题适配器的绑定(bind)方法对我有用的是改变这条线
navController = Navigation.findNavController(itemView)
至
navController = Navigation.findNavController(activity, R.id.nav_host_fragment)
关于android - ConstraintLayout 没有为在 RecyclerView 上单击的项目设置 NavController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60962539/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!