我在尝试返回对象时遇到错误。 该错误来自onClick函数,该函数转到ProductActivity。 我试图通过参数传递产品ID数据,我做错了或为什么我得到这个错误? 错误是:
android.content.ActivityNotFoundException:无法找到显式 活动课 {com.example.cmd.testproject / com.example.cmd.testproject.Fragments.ProductFragment}; 你有没有在AndroidManifest.xml中声明这个活动?
public class ProductListHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
private TextView mTitle,mDesc,mPrice;
private ImageView imageView;
private Product mProduct;
public void bind(Product product) {
this.mProduct = product;
mTitle.setText(product.getTitle());
mDesc.setText(product.getDesc());
mPrice.setText(product.getPrice());
//TODO: Set Image
}
public ProductListHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item,parent,false));
mTitle = (TextView) itemView.findViewById(R.id.title);
mDesc = (TextView) itemView.findViewById(R.id.description);
mPrice = (TextView) itemView.findViewById(R.id.price);
itemView.setOnClickListener(this);
// imageView = (ImageView) itemView.findViewById(R.id.imgUrl);
}
@Override
public void onClick(View v) {
Intent i = ProductActivity.newIntent(ctx,mProduct.getId());
ctx.startActivity(i);
}
}
}
public class ProductActivity extends SingleFragmentActivity {
public static final String PRODUCT_ID = "com.example.cmd.testproject.Activitys.productId";
@Override
protected Fragment createFragment() {
UUID id = (UUID)getIntent().getSerializableExtra(PRODUCT_ID);
return ProductFragment.newInstance(id);
}
public static Intent newIntent(Context ctx, UUID prodID) {
Intent i = new Intent(ctx, ProductFragment.class);
i.putExtra(PRODUCT_ID,prodID);
return i;
}
}
public class ProductFragment extends Fragment {
private static final String ARGUMENT_PROD_ID = "prod_id";
private TextView mTitle,mDesc,mImgUrl,mPrice;
private List<Product> mProducts;
private Product product;
@Override
protected Fragment createFragment() {
UUID id = (UUID)getIntent().getSerializableExtra(PRODUCT_ID);
return ProductFragment.newInstance(id);
}
public static Intent newIntent(Context ctx, UUID prodID) {
Intent i = new Intent(ctx, ProductFragment.class);
i.putExtra(PRODUCT_ID,prodID);
return i;
}
}
public static Fragment newInstance(UUID productID) {
Bundle args = new Bundle();
args.putSerializable(ARGUMENT_PROD_ID,productID);
ProductFragment frag = new ProductFragment();
frag.setArguments(args);
return frag;
}
public ProductFragment() {}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID id = (UUID) getArguments().getSerializable(ARGUMENT_PROD_ID);
product = ProductHolder.get(getActivity()).getProduct(id);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.product_fragment,container,false);
setWidgets(v);
return v;
}
private void setDataOnText(){
mTitle.setText(product.getTitle());
mDesc.setText(product.getDesc());
mPrice.setText(product.getPrice());
}
private void setWidgets(View v) {
mTitle = (TextView) v.findViewById(R.id.title);
mDesc = (TextView) v.findViewById(R.id.desc);
mPrice = (TextView) v.findViewById(R.id.price);
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cmd.testproject">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activitys.ProductActivity">
</activity>
<activity android:name=".Activitys.ProductListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:1)
ProductFragment是一个片段而不是activity.You不能使用intent加载片段。更改newIntent方法来加载片段。
/products/45342