我有MainActivity.class
,它基本上是包含所有产品的网格视图。当用户点击任何这些产品时,它会将他带到详细信息页面,其中列出了产品详细信息。
我能够将响应对象看作一个字符串,需要将此字符串传递给ProductDetailActivity.class
,我在ProductDetailActivity.class
中设置了一个断点,但它永远不会出现。我不确定我是在遗漏某些东西还是做错了什么。
MainActivity.class
final String URL = ".../productDetail.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// response string received successfully and displayed on the console.
VolleyLog.v("Response:%n %s", response);
Intent intent = new Intent(MainActivity.this,ProductDetailActivity.class);
intent.putExtra("jsonArray", response);
MainActivity.this.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}
) {
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
params.put("id", records.get(position).getId());
return params;
}
};
CustomVolleyRequest.getInstance(this).getRequestQueue().add(postRequest);
ProductDetailActivity.class
public class ProductDetailActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.productdetail);
// break point placed in the following line, but never hit!
String jsonArray = getIntent().getStringExtra("jsonArray");
}
}
更新
我仔细调试之后,MainActivity.this.startActivity(intent);那行代码给了我以下错误:
无法找到显式活动
答案 0 :(得分:2)
您是否在ProductDetailActivity
中声明了AndroidManifest.xml
?
< activity android:name="[path to you class].ProductDetailActivity.class" android:configChanges="keyboard|keyboardHidden" >
< /activity>
而且,您确定何时请求&#34; URL =&#34; ... / productDetail.php&#34 ;;&#34;,onResponse函数必须调用
答案 1 :(得分:0)
我就是这样做的:
Intent intent = new Intent(MainActivity.this,ProductDetailActivity.class);
Bundle extras = new Bundle();
extras.putString("jsonArray", response);
intent.putExtras(extras);
startActivity(intent);
然后用:
打开它Bundle extras = getIntent().getExtras();
String jsonArray = extras.getString("jsonArray");