在RecolyerView
CustomViewWholder
内的OnClickListener()
中尝试将我的包放入我的意图时出错。
java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.os.bundle.putString(java.lang.String,java.lang.String)”
我也尝试创建一个新的捆绑包,而不是使用额外的,但得到了与上面相同的错误。奇怪的是,putExtras()
方法可以很好地处理一个变量,并且可以正确地传递,但是,唉,我有大约8个不同的变量要插入辅助页面。
代码如下:
public void onClick(View view) {
intent = new Intent(itemView.getContext(), CardActivity.class);
extras = intent.getExtras();
extras.putString(CARD_IMG, cardList.Basic.get(getAdapterPosition()).getImg());
extras.putString(CARD_FLAVOR, cardList.Basic.get(getAdapterPosition()).getFlavor());
extras.putString(CARD_TEXT, cardList.Basic.get(getAdapterPosition()).getText());
extras.putString(CARD_TYPE, cardList.Basic.get(getAdapterPosition()).getType());
extras.putString(CARD_ARTIST, cardList.Basic.get(getAdapterPosition()).getArtist());
extras.putString(CARD_SET, cardList.Basic.get(getAdapterPosition()).getCardSet());
extras.putString(CARD_RARITY, cardList.Basic.get(getAdapterPosition()).getRarity());
extras.putString(CARD_NAME, cardList.Basic.get(getAdapterPosition()).getName());
intent.putExtras(extras);
itemView.getContext().startActivity(intent);
}
我们将非常感谢在正确的方向上提供任何帮助或推动。
public class RecyclerAdaptor extends RecyclerView.Adapter<RecyclerAdaptor.CustomViewHolder> {
//for bundle keys
private static final String CARD_FLAVOR = "CARD_FLAVOR";
private static final String CARD_TYPE = "CARD_TYPE";
private static final String CARD_ARTIST = "CARD_ARTIST";
private static final String CARD_SET = "CARD_SET";
private static final String CARD_RARITY = "CARD_RARITY";
private static final String CARD_NAME = "CARD_NAME";
private static final String CARD_IMG = "CARD_IMG";
private static final String CARD_TEXT = "CARD_TEXT";
private static Intent intent;
public static Bundle extras;
public static CardList cardList;
public RecyclerAdaptor(CardList cardList) {
this.cardList = cardList;
}
@Override
//Number of Items
public int getItemCount() {
return cardList.Basic.size();
}
@Override
public RecyclerAdaptor.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_row, parent, false);
CustomViewHolder holder = new CustomViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(RecyclerAdaptor.CustomViewHolder holder, int position) {
final Card card = cardList.Basic.get(position);
String cost = "0";
// Could have made values protected in class, but meh
holder.cardName.setText(card.getName());
holder.cardText.setText(card.getText());
holder.cardFlavor.setText(card.getFlavor());
// Handles dust value of card
if (card.getRarity() != null) {
switch (card.getRarity()) {
case "Common": cost = "40";
break;
case "Rare": cost = "100";
break;
case "Epic": cost = "400";
break;
case "Legendary": cost = "1600";
break;
}
}
holder.cardCost.setText(cost);
Picasso.with(holder.cardImg.getContext()).load(card.getImg()).into(holder.cardImg);
}
public static class CustomViewHolder extends RecyclerView.ViewHolder {
private TextView cardName;
private TextView cardText;
private TextView cardFlavor;
private TextView cardCost;
private GifImageView cardImg;
private Button moreButton;
public CustomViewHolder(final View itemView) {
super(itemView);
cardName = itemView.findViewById(R.id.cardTitle);
cardText = itemView.findViewById(R.id.cardText);
cardFlavor = itemView.findViewById(R.id.cardFlavor);
cardCost = itemView.findViewById(R.id.dustTextView);
cardImg = itemView.findViewById(R.id.cardImgView);
moreButton = itemView.findViewById(R.id.moreButton);
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(itemView.getContext(), "Clicked:" + getAdapterPosition(), Toast.LENGTH_SHORT).show();
intent = new Intent(itemView.getContext(), CardActivity.class);
extras = intent.getExtras();
if(extras == null){
extras = new Bundle();
}
extras.putString(CARD_IMG, cardList.Basic.get(getAdapterPosition()).getImg());
extras.putString(CARD_FLAVOR, cardList.Basic.get(getAdapterPosition()).getFlavor());
extras.putString(CARD_TEXT, cardList.Basic.get(getAdapterPosition()).getText());
extras.putString(CARD_TYPE, cardList.Basic.get(getAdapterPosition()).getType());
extras.putString(CARD_ARTIST, cardList.Basic.get(getAdapterPosition()).getArtist());
extras.putString(CARD_SET, cardList.Basic.get(getAdapterPosition()).getCardSet());
extras.putString(CARD_RARITY, cardList.Basic.get(getAdapterPosition()).getRarity());
extras.putString(CARD_NAME, cardList.Basic.get(getAdapterPosition()).getName());
intent.putExtras(extras);
itemView.getContext().startActivity(intent);
}
});
}
}
}
此外,如果我保持一切原样,去掉原意.putextras的话,它就不会崩溃。
这是因为您的意图
为空
,请替换为下面的代码
public void onClick(View view) {
intent = new Intent(itemView.getContext(), CardActivity.class);
extras = new Bundle(); //<-- so that bundle wont be null anymore
extras.putString(CARD_IMG, cardList.Basic.get(getAdapterPosition()).getImg());
extras.putString(CARD_FLAVOR, cardList.Basic.get(getAdapterPosition()).getFlavor());
extras.putString(CARD_TEXT, cardList.Basic.get(getAdapterPosition()).getText());
extras.putString(CARD_TYPE, cardList.Basic.get(getAdapterPosition()).getType());
extras.putString(CARD_ARTIST, cardList.Basic.get(getAdapterPosition()).getArtist());
extras.putString(CARD_SET, cardList.Basic.get(getAdapterPosition()).getCardSet());
extras.putString(CARD_RARITY, cardList.Basic.get(getAdapterPosition()).getRarity());
extras.putString(CARD_NAME, cardList.Basic.get(getAdapterPosition()).getName());
intent.putExtras(extras);
itemView.getContext().startActivity(intent);
}
问题内容: 编辑:Symfony最佳做法回答了我的大部分问题。 关于我的Symfony2应用程序,我有几个问题。 它将有一个前端和一个后端,并且它们将使用一些通用代码(例如日期显示器,分页器,一些经常使用的模板等)。 因此,我创建了一个FrontendBundle和一个BackendBundle,它们分别包含各自的布局。第一个问题:为前端和后端创建捆绑包(这是甚至没有控制器的“通用”捆绑包)的优良
在本章中,您将学习如何在Aurelia框架中使用捆绑。 第1步 - 安装先决条件 您可以通过在command prompt运行以下命令来安装aurelia-bundler 。 C:\Users\username\Desktop\aureliaApp>npm install aurelia-bundler --save-dev 如果您没有安装gulp,可以通过运行此代码来安装它。 C:\Users
正在下载.../DLL的/Dotnetzip.dll(458 KB)下载对象时出错:.../DLL的/Dotnetzip.dll(7DD20A2):Smudge错误:下载错误.../DLL的/Dotnetzip.dll(7DD20A2291B05323BBA04BE4AE656D7635AE5E68A5A6FA2B9F86E27841846A31):批处理请求:缺少协议:“C:/...bundle
问题内容: 在Symfony2应用的路由配置中,我可以引用这样的文件: 有什么方法可以访问控制器或其他PHP代码中与包相关的文件?特别是,我尝试使用Symfony \ Component \ Yaml \ Parser对象来解析文件,并且我不想绝对引用该文件。本质上,我想这样做: 我已经签出了Symfony \ Component \ Finder \ Finder类,但我认为这不是我想要的。有任
我试图在Felix中添加一个新的OSGI包(没有Karaf)作为ActiveMQ客户端(消息发送方)。 ActiveMQ客户端在非OSGI环境中运行良好。但当我在Felix中使用相同的代码(稍加修改)时,它会抛出以下错误: org.osgi.framework.捆绑异常:无法解决com.packtpub.felix.bookshelf-库存-impl-mock[7](R 7.3):缺少需求[com
我正在将我的spring-java项目转移到OSGi。 我有一些依赖项,这些依赖项在spring ebr repo或maven repo中不能作为包提供。处理它们的最佳方法是什么? null