你好我的情况是我正在创建一个列表,一旦单击列表中的项目,它必须将用户发送到另一个片段,该片段将在我的应用程序中的谷歌地图中有占位符。我的问题是,当我点击列表中项目上的按钮时,应用程序立即崩溃。我的日志告诉我我有一个空例外。我知道我已经在我的观点和听众之间建立了一个参考,但希望对这个问题有更深入的了解。 任何帮助将不胜感激,谢谢!
fragment_countries.xml
public class Foo extends HashMap<Class, List>{
@Override
public List put(Class clazz, List list){
...
// make sure every thing in list is of type clazz here
return super.put(clazz, list);
}
@Override
public List get(Object clazz){
return super.get(clazz);
}
}
row.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="myapps.countryapp.Countries"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/MyListView">
</ListView>
</FrameLayout>
Countries.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/ImageViewPicasso"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textStyle="bold"
android:gravity="center_vertical"
android:id="@+id/textAreaList"
android:padding="10dp"/>
</LinearLayout>
MyMapFragment.java
package myapps.countryapp;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.squareup.picasso.Picasso;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class Countries extends Fragment {
private ListView mListView;
private List<CountriesRetrieved> retrievedCountriesList;
public Countries() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentview = inflater.inflate(R.layout.fragment_countries, container, false);
mListView = (ListView) fragmentview.findViewById(R.id.MyListView);
retrievedCountriesList = new ArrayList<>(); //Array of objects.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Countries"); //name of the database from parse.com
query.findInBackground(new FindCallback<ParseObject>() {
// @Override
public void done(List<ParseObject> countriesList, com.parse.ParseException e) {
if (e == null) {
for (ParseObject parseObject : countriesList) {
String titleOfCountry = (String) parseObject.get("Title");
String imageLink = (String) parseObject.get("Image_URL");
String latitude = (String) parseObject.get("Latitude");
String longitude = (String) parseObject.get("Longitude");
CountriesRetrieved countries = new CountriesRetrieved(titleOfCountry, imageLink, latitude, longitude);
retrievedCountriesList.add(countries);
}
mListView.setAdapter(new CountriesAdapterList());
} else {
Log.d("Cannot Parse", "Error: " + e.getMessage());
}
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CountriesRetrieved crl = retrievedCountriesList.get(position);
Double lat = Double.parseDouble(crl.getLatitude());
Double lon = Double.parseDouble(crl.getLongitude());
MyMapFragment mp = new MyMapFragment();
mp.setLocation(lat, lon);
}
});
return fragmentview;
}
private class CountriesAdapterList extends BaseAdapter{
@Override
public int getCount() {
return retrievedCountriesList.size(); //This is how the adaptor keeps going, because of the size of list.
}
@Override
public Object getItem(int position) {
return retrievedCountriesList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = getActivity().getLayoutInflater().inflate(R.layout.row, null);//Instantiates a layout XML file into its corresponding View objects.
CountriesRetrieved rowCountries = retrievedCountriesList.get(position);
TextView textViewRow = (TextView) rowView.findViewById(R.id.textAreaList); //Find text view on every row.
textViewRow.setText(rowCountries.getTitle());
ImageView imageView = (ImageView) rowView.findViewById(R.id.ImageViewPicasso);
Picasso.with(getActivity()).load(rowCountries.getImageLink()).into(imageView);
return rowView;
}
}
}
我的日志猫:
package myapps.countryapp;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/**
* Created by user on 8/25/2015.
*/
public class MyMapFragment extends SupportMapFragment {
GoogleMap googleMap = getMap();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void setLocation(Double latitude, Double longitude){
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(latitude, longitude));
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.set_marker));
googleMap.addMarker(markerOptions);
}
}