尝试转到新的Intent时Android App崩溃了

时间:2017-03-03 08:40:25

标签: android api android-studio android-intent crash

我正在尝试构建一个应用程序,以便从列表的项目到另一个具有意图的屏幕,但它会一直崩溃。

只要按下列表项并尝试转到新活动,应用就会崩溃。

MAIN_ACTIVITY.JAVA

public class MainActivity extends AppCompatActivity {
   String url = "http://www.[mydomain].com/wordpress/wp-json/wp/v2/posts/?per_page=99&fields=id,title";
   List<Object> list;
   Gson gson;
   ProgressDialog progressDialog;
   ListView postList;
   Map<String,Object> mapPost;
   Map<String,Object> mapTitle;
   int postID;
   String postTitle[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    postList = (ListView)findViewById(R.id.postList);
    progressDialog = new ProgressDialog(MainActivity.this);
    progressDialog.setMessage("Loading...");
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.show();

    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            gson = new Gson();
            list = (List) gson.fromJson(s, List.class);
            postTitle = new String[list.size()];

            for(int i=0;i<list.size();++i){
                mapPost = (Map<String,Object>)list.get(i);
                mapTitle = (Map<String, Object>) mapPost.get("title");
                postTitle[i] = (String) mapTitle.get("rendered");
            }

            postList.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,postTitle));
            progressDialog.dismiss();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(MainActivity.this, "Some error occurred", Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
    rQueue.add(request);

    postList.setOnItemClickListener(
            new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mapPost = (Map<String,Object>)list.get(position);
            postID = ((Double)mapPost.get("id")).intValue();
            Toast.makeText(MainActivity.this, "Position: " + list.get(position) + ", ID: " + postID, Toast.LENGTH_LONG).show();

            **THE PROBLEM LIES HERE! It crashes when I click on the list item, to go to the content...**
            Intent intent = new Intent(getApplicationContext(), Post.class);
            intent.putExtra("id", "" + 
            startActivity(intent);
        }

    });
}

}

3 个答案:

答案 0 :(得分:0)

检查清单文件中的帖子活动声明

  Intent intent = new Intent(MainActivity.this, Post.class);
  startActivity(intent);

答案 1 :(得分:0)

**THE PROBLEM LIES HERE! It crashes when I click on the list item, to go to the content...**
    Intent intent = new Intent(MainActivity.this, Post.class); // use MainActivity.this
    intent.putExtra("id", yourId); // seems typo
    startActivity(intent);

请同时查看AndroidManifest.xml。

活动&#34;发布&#34;应该在那里宣布。

<activity android:name=".Post" />

答案 2 :(得分:0)

Intent intent = new Intent(getApplicationContext(), Post.class);
            intent.putExtra("id", "" +   // **INCOMPLETE**
            startActivity(intent);

//在清单中声明活动