我想将数据从RecyclerView发送到Activity,但是从RecyclerView的Arraylist中获取的 Image Data 没有发送到Activity。
我成功地编译了代码,但是每当我打开应用程序并在Recycleriew中选择1个项目时,该应用程序都会被强制关闭。
我希望图像和描述数据显示在“活动”中,但始终会强制关闭该应用程序。
片段类:
public class ShipFragment extends Fragment {
private View v;
private RecyclerView recyclerShip;
private ArrayList<Warship> shipList = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.ship_fragment, container,false);
recyclerShip = (RecyclerView) v.findViewById(R.id.Recycler_id);
shipList.addAll(ShipDataRecycler.getShipList());
recyclerShip.setHasFixedSize(true);
showRecyclerShipList();
return v;
}
private void showRecyclerShipList() {
RecyclerAdapter recyclerAdapter;
recyclerAdapter = new RecyclerAdapter(shipList);
recyclerShip.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerShip.setAdapter(recyclerAdapter);
recyclerAdapter.setOnItemClickCallback(new RecyclerAdapter.OnItemClickCallback() {
@Override
public void onItemClick(Warship zListShip) {
showSelectedShip(zListShip);
}
});
}
private void showSelectedShip(Warship ship){
Intent swap = new Intent(getActivity(), Detail_item.class);
swap.putExtra(Detail_item.image,ship.getImges());
swap.putExtra(Detail_item.title, ship.getzTitle());
swap.putExtra(Detail_item.description, ship.getzDescription());
startActivity(swap);
}
}
活动分类:
public class Detail_item extends AppCompatActivity {
private ImageView imgDetail;
private TextView titleDetail, descDetail;
private ArrayList<Warship> listWarship;
public static final String title = "Nama";
public static final String description = "deskripsi";
public static final String image = "foto";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_item);
imgDetail = findViewById(R.id.profil_img_id);
titleDetail = findViewById(R.id.titleShip_id);
descDetail = findViewById(R.id.desc_id);
String imageShip = getIntent().getStringExtra(image);
String titleShip = getIntent().getStringExtra(title);
String descShip = getIntent().getStringExtra(description);
titleDetail.setText(titleShip);
imgDetail.setImageResource(Integer.parseInt(imageShip));
descDetail.setText(descShip);
}
}
崩溃日志:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.kimino.azurlaneshipreview / com.kimino.azurlaneshipreview.Detail_item}: java.lang.NumberFormatException:空 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2706) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1514) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:163) 在android.app.ActivityThread.main(ActivityThread.java:6205) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 引起原因:java.lang.NumberFormatException:null 在java.lang.Integer.parseInt(Integer.java:483) 在java.lang.Integer.parseInt(Integer.java:556) 在com.kimino.azurlaneshipreview.Detail_item.onCreate(Detail_item.java:38) 在android.app.Activity.performCreate(Activity.java:6864) 在> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)>在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
答案 0 :(得分:0)
由于
导致出现错误 String imageShip = getIntent().getStringExtra(image);
返回一个空值。运行Integer.parseInt(imageShip)
时,您会得到NullFormatException
。
您应该确保要分配
public static final String image = "foto";
作为分配附加值时的键
//make sure that Detail_item.image == "foto" or that when you get the
//extra, you are using the same key.
swap.putExtra(Detail_item.image,ship.getImges());
当前该请求返回null
。