gpt4 book ai didi

android - Android应用在startActivity崩溃( Intent )

转载 作者:行者123 更新时间:2023-12-03 17:06:41 25 4
gpt4 key购买 nike

通过意图将数据传递到另一个 Activity 时,应用程序崩溃,部分代码如下:

public class Walls extends Fragment {

private GridView gridView;
private GridViewAdapter gridAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = inflater.inflate(R.layout.walls, container, false);
gridView = (GridView) view.findViewById(R.id.gridView);
gridAdapter = new GridViewAdapter(getContext(), R.layout.grid_item_layout, getData());
gridView.setAdapter(gridAdapter);

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageItem item = (ImageItem) parent.getItemAtPosition(position);

//Create intent
Intent intent = new Intent(Walls.this.getActivity(), DetailsActivity.class);
intent.putExtra("title", item.getTitle());
intent.putExtra("image", item.getImage());

//Start details activity
startActivity(intent);
}
});

...
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".DetailsActivity"/>

...

该应用程序崩溃了,没有任何Android默认弹出窗口,即“应用程序已停止”

知道是什么问题吗?

最佳答案

getActivity()上用于Fragment的上下文。它可能是NullpointerException,因此请打印日志并进行检查。

  gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageItem item = (ImageItem) parent.getItemAtPosition(position);

Intent intent = new Intent(getActivity(), DetailsActivity.class);
intent.putExtra("title", item.getTitle());
intent.putExtra("image", item.getImage());

Log.e("title",""+ item.getTitle());
Log.e("image",""+ item.getImage());

startActivity(intent);
}
});

关于android - Android应用在startActivity崩溃( Intent ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48600793/

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