gpt4 book ai didi

java - 为什么我不能将此 JSON 对象传递给另一个 Activity

转载 作者:行者123 更新时间:2023-11-29 19:57:48 50 4
gpt4 key购买 nike

我正在使用 Volley 从 Json 获取新闻项目。在 MainActivity 中,我已经成功获取并解析了 json,所以我对此没有问题。现在每条新闻都包含标题和新闻摘录。我想要的是每次点击新闻摘录时,它都会启动另一个名为 NewsDetails 的 Activity , 启动时,点击新闻摘录的“ID”(一个json对象),将被发送到NewsDetails

NewsDetails 中,我将连接“ID”和网站的 url;这也将返回一个 json 数组。然后我会解析json并显示它;希望这不会给我带来问题。

我遇到困难的地方是尝试发送“ID”并启动“NewsDetails”。

到目前为止,这是我一直在做的:

所有 MainActivity 类

  public class MainActivity extends AppCompatActivity{

private final String TAG = "MainActivity";

//Creating a list of newss
private List<NewsItems> mNewsItemsList;

//Creating Views
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private ProgressDialog mProgressDialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Device rotated and onCreate called");

//Initializing Views
recyclerView = (RecyclerView) findViewById(R.id.news_recycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);



//Initializing the newslist
mNewsItemsList = new ArrayList<>();
adapter = new NewsAdapter(mNewsItemsList, this);

recyclerView.setAdapter(adapter);



//Caling method to get data
getData();

}


//This method will get data from the web api

private void getData(){


Log.d(TAG, "getData called");
//Showing progress dialog
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setCancelable(false);
mProgressDialog.setMessage(this.getResources().getString(R.string.load_news));
mProgressDialog.show();


//Creating a json request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(ConfigNews.GET_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, "onResponse called");
//Dismissing the progress dialog
if (mProgressDialog != null) {
mProgressDialog.hide();
}
/*progressDialog.dismiss();*/


//calling method to parse json array
parseData(response);

}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);

//Adding request to the queue
requestQueue.add(jsonArrayRequest);

}

//This method will parse json data
private void parseData(JSONArray array){
Log.d(TAG, "Parsing array");

for(int i = 0; i<array.length(); i++) {
NewsItems newsItem = new NewsItems();
JSONObject jsonObject = null;
try {
jsonObject = array.getJSONObject(i);
newsItem.setNews_title(jsonObject.getString(ConfigNews.TAG_NEWS_TITLE));
newsItem.setNews_excerpt(jsonObject.getString(ConfigNews.TAG_NEWS_EXCERPT));
newsItem.setNewsId(jsonObject.getInt(ConfigNews.TAG_NEWS_ID));

} catch (JSONException w) {
w.printStackTrace();
}
mNewsItemsList.add(newsItem);

}

adapter.notifyItemRangeChanged(0, adapter.getItemCount());


}


@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy called");
if (mProgressDialog != null){
mProgressDialog.dismiss();
Log.d(TAG, "mProgress dialog dismissed");

}
}

}

上面的代码没有问题。

整个 NewsItems 类

public class NewsItems {
private String news_title;
private String news_excerpt;
private String news_id;

public String getNews_title() {
return news_title;
}

public void setNews_title(String news_title) {
this.news_title = news_title;
}

public String getNews_excerpt() {
return news_excerpt;
}

public void setNews_excerpt(String news_excerpt) {
this.news_excerpt = news_excerpt;
}

public String getNews_id() {
return news_id;
}

public void setNews_id(String news_id) {
this.news_id = news_id;
}

}

上面的代码仍然没有问题。

所有 NewsAdapter

   public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private ImageLoader imageLoader;
private Context mContext;

//List of newss
private List<NewsItems> mNewsItems;

private final int VIEW_ITEM = 0;
private final int VIEW_PROG = 1;

private int lastPosition = -1;

public NewsAdapter(List<NewsItems> newsItems, Context context) {
super();

//Getting all newss
this.mNewsItems = newsItems;
this.mContext = context;
}

@Override
public int getItemViewType(int position) {
if (isPositionItem(position))
return VIEW_ITEM;
return VIEW_PROG;
}

private boolean isPositionItem(int position) {
return position != getItemCount()-1;
}



@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_summ, parent, false);
return new TextViewHolder(v);
} else if (viewType == VIEW_PROG){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerfooter, parent, false);
return new ProgressViewHolder(v);
}

return null;
}



@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


if (holder instanceof TextViewHolder) {
NewsItems newsList = mNewsItems.get(position);
((TextViewHolder) holder).newsTitle.setText(newsList.getNews_title());
((TextViewHolder) holder).newsExcerpt.setText(newsList.getNews_excerpt());
((TextViewHolder) holder).newsId.setText(String.valueOf(newsList.getNewsId()));

} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
}


}


@Override
public int getItemCount(){
//Return the number of items in the data set
return mNewsItems.size();
}




public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView newsTitle,newsExcerpt, newsId;
public ImageButton imageButton;
public NewsItems dNewsItems;

public TextViewHolder (final View newsView) {
super(newsView);
newsTitle = (TextView) newsView.findViewById(R.id.news_title);
newsExcerpt = (TextView) newsView.findViewById(R.id.news_excerpt);
newsId = (TextView) newsView.findViewById(R.id.news_id);

newsExcerpt.setOnClickListener(this);


}



@Override
public void onClick(View v) {


if (v.getId() == newsExcerpt.getId()) {
if (v.getId() == newsExcerpt.getId()) {
Intent i = NewsDetails.newIntent(getActivity(), dNewsItems.getNews_id());
startActivity(i);
}
}
}
}

public static class ProgressViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
Button loadButton;
ProgressBar progressBar;
public ProgressViewHolder(View footerView){
super(footerView);
loadButton = (Button) footerView.findViewById(R.id.reload_button);
progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load);

loadButton.setOnClickListener(this);

if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
progressBar.setVisibility(View.VISIBLE);


} else if (!NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
loadButton.setVisibility(View.VISIBLE);
}
}

@Override
public void onClick(View v) {
if (v.getId() == loadButton.getId()) {
//
}
}
}


}

这是给我带来问题的部分。

具体线路

Intent i = NewsDetails.newIntent(getActivity(), dNewsItems.getNews_id());
startActivity(i);

当我将鼠标悬停在 newIntent 上时,它显示 “无法解析方法 'newIntent(?, java.lang.String)'”。然后我将鼠标悬停在 getActivity() 上,它说 “无法解析方法 'getActivity()'”。对于 startActivity(i);,它是 “无法解析方法 'startActivity(android.content.Intent)'”

那么现在,我的问题是我做错了什么以及我应该如何纠正它?

实现 Rohit Arya 的回答后的 NewsAdapter

public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private ImageLoader imageLoader;
private Context mContext;

//List of newss
private List<NewsItems> mNewsItems;

private final int VIEW_ITEM = 0;
private final int VIEW_PROG = 1;

private int lastPosition = -1;

public NewsAdapter(List<NewsItems> newsItems, Context context) {
super();

//Getting all newss
this.mNewsItems = newsItems;
this.mContext = context;
}

@Override
public int getItemViewType(int position) {
if (isPositionItem(position))
return VIEW_ITEM;
return VIEW_PROG;
}

private boolean isPositionItem(int position) {
return position != getItemCount()-1;
}



@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if (viewType == VIEW_ITEM) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_summ, parent, false);
return new TextViewHolder(v, mContext);
} else if (viewType == VIEW_PROG){
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerfooter, parent, false);
return new ProgressViewHolder(v);
}

return null;
}



@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


if (holder instanceof TextViewHolder) {
NewsItems newsList = mNewsItems.get(position);
((TextViewHolder) holder).newsTitle.setText(newsList.getNews_title());
((TextViewHolder) holder).newsExcerpt.setText(newsList.getNews_excerpt());
((TextViewHolder) holder).newsId.setText(String.valueOf(newsList.getNewsId()));

} else {
((ProgressViewHolder) holder).progressBar.setIndeterminate(true);
((ProgressViewHolder) holder).loadButton.setText(R.string.reload);
}


}


@Override
public int getItemCount(){
//Return the number of items in the data set
return mNewsItems.size();
}




public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView newsTitle,newsExcerpt, newsId;
public ImageButton imageButton;
public NewsItems dNewsItems;
private Context context;

public TextViewHolder (final View newsView, final Context context) {
super(newsView);
newsTitle = (TextView) newsView.findViewById(R.id.news_title);
newsExcerpt = (TextView) newsView.findViewById(R.id.news_excerpt);
newsId = (TextView) newsView.findViewById(R.id.news_id);

newsExcerpt.setOnClickListener(this);


}



@Override
public void onClick(View v) {


if (v.getId() == newsExcerpt.getId()) {
int newsid = 4536;
Intent myItent = new Intent(context, NewsDetails.class);
myItent.putExtra("key", newsid);
context.startActivity(myItent);
}
}
}
}

public static class ProgressViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
Button loadButton;
ProgressBar progressBar;
public ProgressViewHolder(View footerView){
super(footerView);
loadButton = (Button) footerView.findViewById(R.id.reload_button);
progressBar = (ProgressBar) footerView.findViewById(R.id.progress_load);

loadButton.setOnClickListener(this);

if(NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
progressBar.setVisibility(View.VISIBLE);


} else if (!NetworkCheck.isAvailableAndConnected(footerView.getContext())) {
loadButton.setVisibility(View.VISIBLE);
}
}

@Override
public void onClick(View v) {
if (v.getId() == loadButton.getId()) {
//
}
}
}


}

来自实现 Rohit Arya 的答案的 Stacktrace

04-12 20:15:25.424 8772-8772/com.newsus.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.newsus.app, PID: 8772
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4336)
at com.newsus.app.NewsAdapter$TextViewHolder.onClick(NewsAdapter.java:181)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

在第 81 行的堆栈跟踪中,对应于他的回答的 Intent myIntent = new Intent(context, NewsDetails.class); 行。

最佳答案

像这样更改 TextViewHolder 的构造函数以接收 context:

public static class TextViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView newsTitle, newsExcerpt;
public ImageButton imageButton;
public NewsItems dNewsItems;
private Context context;

public TextViewHolder (final View newsView, final Context context) {
super(newsView);
this.context = context; // EDIT 1: You are missing this
newsTitle = (TextView) newsView.findViewById(R.id.news_title);
newsExcerpt = (TextView) newsView.findViewById(R.id.news_excerpt);
newsExcerpt.setOnClickListener(this);
}

现在使用此上下文来启动这样的 Activity :

Intent myIntent = new Intent(context, NewsDetails.class);
myIntent.putExtra("key", value); //Optional parameters
context.startActivity(myIntent);

关于java - 为什么我不能将此 JSON 对象传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36556147/

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