gpt4 book ai didi

java - Android ListView 与详细信息页空对象

转载 作者:太空宇宙 更新时间:2023-11-04 11:14:12 26 4
gpt4 key购买 nike

我正在从 api 的内容创建一个 ListView ,如果我单击一个 ListView 项目,我想显示一个新 View ,其中包含该单击项目的更多详细信息,当前 ListView 可以正确显示,但如果我单击一个项目,应用程序崩溃,并且收到此错误消息:

> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{de.dev.app/de.dev.app.ui.quote.ArticleDetailActivity}:
> java.lang.NullPointerException: Attempt to invoke virtual method
> 'java.lang.String de.dev.app.jokeapp.entities.Joke.getTitle()' on a null
> object reference ... Caused by: java.lang.NullPointerException:
> Attempt to invoke virtual method 'java.lang.String
> de.dev.app.entities.Joke.getTitle()' on a null object reference
> at
> de.dev.app.ui.quote.ArticleDetailFragment.onCreateView(ArticleDetailFragment.java:100)

错误指向我的 ArticleDetailFragment.java 中的这一行:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflateAndBind(inflater, container, R.layout.fragment_article_detail);
if (!((BaseActivity) getActivity()).providesActivityToolbar()) {
((BaseActivity) getActivity()).setToolbar((Toolbar) rootView.findViewById(R.id.toolbar));
}
collapsingToolbar.setTitle(jokeItem.getTitle()); // points here
author.setText(jokeItem.getTitle());
quote.setText(jokeItem.getTitle());
jokeHeader.setText(jokeItem.getTitle());
jokeContent.setText(jokeItem.getContent());

return rootView;
}

这是我的 onAttach 方法:

@Override
public void onAttach(Context context) {
super.onAttach(context);
Bundle bundle = getArguments();
if(bundle == null) {
getActivity().finish();
return;
}
jokeItem = (Joke)bundle.getSerializable("joke");
}

这是我的 ArticleDetailFragment 看起来像:

public class ArticleDetailFragment extends BaseFragment {



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getArguments().containsKey(ARG_ITEM_ID)) {
// load dummy item by using the passed item ID.
dummyItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
}

SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
tokenManager = TokenManager.getInstance(preferences);
service = RetrofitBuilder.createServiceWithAuth(ApiService.class, tokenManager);

setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflateAndBind(inflater, container, R.layout.fragment_article_detail);
if (!((BaseActivity) getActivity()).providesActivityToolbar()) {
// No Toolbar present. Set include_toolbar:
((BaseActivity) getActivity()).setToolbar((Toolbar) rootView.findViewById(R.id.toolbar));
}
collapsingToolbar.setTitle(jokeItem.getTitle());
author.setText(jokeItem.getTitle());

return rootView;
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.sample_actions, menu);
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// your logic
return true;
}
return super.onOptionsItemSelected(item);
}


@Override
public void onAttach(Context context) {
super.onAttach(context);
Bundle bundle = getArguments();
if(bundle == null) {
getActivity().finish();
return;
}
jokeItem = (Joke)bundle.getSerializable("joke");
}

}

我的文章详情 Activity

public class ArticleDetailActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

ArticleDetailFragment fragment = ArticleDetailFragment.newInstance(getIntent().getStringExtra(ArticleDetailFragment.ARG_ITEM_ID));
getFragmentManager().beginTransaction().replace(R.id.article_detail_container, fragment).commit();
}

@Override
public boolean providesActivityToolbar() {
return false;
}
}

在我的 ListActivit 中调用 ArticleDetailActivity,如下所示:

public class ListActivity extends BaseActivity implements ArticleListFragment.Callback {
...
@Override
public void onItemSelected(Joke joke) {

Intent detailIntent = new Intent(this, ArticleDetailActivity.class);
// detailIntent.putExtra(ArticleDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
}
...

最佳答案

从 ListActivity 中像这样调用 DetailActivity,

Intent detailIntent = new Intent(this, ArticleDetailActivity.class);
// detailIntent.putExtra(ArticleDetailFragment.ARG_ITEM_ID, id);
Bundle bundle = new Bundle();
bundle.putSerializable("joke", joke);
detailIntent.putExtras(bundle);
startActivity(detailIntent);

并像这样更改您的 ArticleDetailActivity 更改,我们需要将数据发送到 fragment

public class ArticleDetailActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);

// Show the Up button in the action bar.
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

ArticleDetailFragment fragment = new ArticleDetailFragment();

fragment.setArguments(getIntent().getExtras());

getFragmentManager().beginTransaction().replace(R.id.article_detail_container, fragment).commit();
}

@Override
public boolean providesActivityToolbar() {
return false;
}
}

关于java - Android ListView 与详细信息页空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661943/

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