应用程序在通过intent将数据传递给另一个活动时崩溃,代码部分如下:
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默认弹出窗口,即&#34;应用已停止&#34;
知道问题是什么?
答案 0 :(得分:0)
在getActivity()
上使用片段的上下文。它可能是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);
}
});
答案 1 :(得分:0)
请参阅getTitle,getImage和getAvtivity的返回值。其中一个必须返回null,因此崩溃。