2017-03-03 103 views
0

我試圖建立一個應用程序從一個列表的項目到另一個屏幕的意圖,但它不斷崩潰。Android應用程序崩潰時,試圖去一個新的意圖

只要我按下列表項並嘗試去新的活動,應用程序崩潰。

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

    }); 
} 

}

+1

你可以發表你的logcat的崩潰報告 –

回答

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中。

活動「帖子」應在那裏申報。

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

非常感謝!有時你會忘記小事情。 – sergej

0

檢查發佈活動申報清單文件

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

//聲明活動的清單

+0

非常感謝!有時你會忘記小事情。 – sergej

+0

@sergej它現在在工作嗎? –

相關問題