【问题标题】:Android App crashes when trying to go to a new Intent尝试转到新 Intent 时,Android 应用程序崩溃
【发布时间】:2017-03-03 08:40:24
【问题描述】:

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

当我按下列表项并尝试转到新 Activity 时,应用程序崩溃。

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

    });
}

}

【问题讨论】:

  • 你能发布你的 logcat 崩溃报告吗

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


【解决方案1】:
**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" />

【讨论】:

  • 非常感谢!有时你会忘记一些小事。
【解决方案2】:

检查清单文件中的 Post Activity 声明

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

【讨论】:

    【解决方案3】:
    Intent intent = new Intent(getApplicationContext(), Post.class);
                intent.putExtra("id", "" +   // **INCOMPLETE**
                startActivity(intent);
    

    //在Manifest中声明Activity

    【讨论】:

    • 非常感谢!有时你会忘记一些小事。
    猜你喜欢
    • 2020-08-08
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多