gpt4 book ai didi

android - 尝试转到新的Intent时,Android App崩溃

转载 作者:行者123 更新时间:2023-12-03 17:11:30 24 4
gpt4 key购买 nike

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

当我按下“列表”项目并尝试转到新的“ 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);
}

});
}

}

最佳答案

**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 “发布”应在此处声明。
<activity android:name=".Post" />

关于android - 尝试转到新的Intent时,Android App崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42574182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com