sergej
sergej

Reputation: 45

Android App crashes when trying to go to a new Intent

I'm trying to build an app to get from the Item of a list to another screen with the intent, however it keeps crashing.

As soon as I press the List item and try to go to the new Activity, the app crashes.

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);
        }

    });
}

}

Upvotes: 1

Views: 3661

Answers (3)

KKSINGLA
KKSINGLA

Reputation: 1344

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

//Declare Activity in Manifest

Upvotes: 0

Alan
Alan

Reputation: 296

**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);

Please also check the AndroidManifest.xml.

Activity "Post" should be declared there.

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

Upvotes: 3

Komal12
Komal12

Reputation: 3348

Check Post Activity declare in manifest file

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

Upvotes: 0

Related Questions