问题
I create the following class who extends MapActivity. The error is in this class here in the line
View view = infalInflater.inflate(R.layout.car_map_view, null);
public class ShowCarMapChild extends MapActivity {
private MapView mapView;
private Car car;
private String tag;
@Override
protected void onCreate(Bundle icicle) {
// TODO Auto-generated method stub
super.onCreate(icicle);
setContentView(R.layout.car_map_view);
}
public void setCar(Car car){
this.car = car;
}
public void setTag (String tag){
this.tag = tag;
}
public String getTag (){
return tag;
}
public View getMapView(Context context){
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = infalInflater.inflate(R.layout.car_map_view, null);
setMap(view,context);
return view;
}
public void setMap(View view, Context context){
mapView = (MapView) view.findViewById(R.id.mapShowCarLocation);
MapController mc = mapView.getController();
mc.animateTo(car.getLocation());
mc.setZoom(15);
PinpointItem point = new PinpointItem(car.getLocation(), car, context);
Drawable d = getResources().getDrawable(R.drawable.pinpoint);
CustomPinpointOverlay overlay = new CustomPinpointOverlay(d);
overlay.insertPinpoint(point);
mapView.getOverlays().add(overlay);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
This is the xml file:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:orientation="vertical" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapShowCarLocation"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0UFOPeLgKxoW8VGjdzV5QN6N53N7733i1kTFI3g"
android:enabled="true" />
</LinearLayout>
And this is the Adapter
public class ShowCarExpandListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<ShowCarExpandGroup> groups;
private Car car;
public ShowCarExpandListAdapter (Context context, ArrayList<ShowCarExpandGroup> groups, Car car){
this.context = context;
this.groups = groups;
this.car = car;
}
public void addItem(ShowCarExpandChild item, ShowCarExpandGroup group){
if (!groups.contains(group)){
groups.add(group);
}
int index = groups.indexOf(group);
ArrayList<ShowCarExpandChild> ch = groups.get(index).getItems();
ch.add(item);
groups.get(index).setItems(ch);
}
public void addItem(ShowCarMapChild item, ShowCarExpandGroup group){
if (!groups.contains(group)){
groups.add(group);
}
int index = groups.indexOf(group);
ArrayList<ShowCarMapChild> ch = groups.get(index).getMapItems();
ch.add(item);
groups.get(index).setMapItems(ch);
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
if (groupPosition == 0){
ArrayList<ShowCarMapChild> chList = groups.get(groupPosition).getMapItems();
return chList.get(childPosition);
}else{
ArrayList<ShowCarExpandChild> chList = groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view,
ViewGroup parent) {
// TODO Auto-generated method stub
if (view == null){
if (groupPosition == 0){
ShowCarMapChild mapChild = (ShowCarMapChild) getChild(groupPosition, childPosition);
view = mapChild.getMapView(context);
//LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
//ShowCarMapChild mapChild = (ShowCarMapChild) getChild(groupPosition, childPosition);
//view = infalInflater.inflate(R.layout.car_map_view, null);
//MapView mv = mapChild.getMap(view, context);
//mv.setTag(mapChild.getTag());
}else{
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
ShowCarExpandChild child = (ShowCarExpandChild) getChild(groupPosition, childPosition);
view = infalInflater.inflate(R.layout.car_child, null);
TextView tv = (TextView) view.findViewById(R.id.tvChild);
tv.setText(child.getName());
tv.setTag(child.getTag());
}
}
return view;
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
if (groupPosition == 0){
ArrayList<ShowCarMapChild> chList = groups.get(groupPosition).getMapItems();
return chList.size();
}else{
ArrayList<ShowCarExpandChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groups.get(groupPosition);
}
public int getGroupCount() {
// TODO Auto-generated method stub
return groups.size();
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
// TODO Auto-generated method stub
ShowCarExpandGroup group = (ShowCarExpandGroup) getGroup(groupPosition);
if(view == null){
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.car_group, null);
}
TextView tv = (TextView) view.findViewById(R.id.tvGroup);
tv.setText(group.getName());
return view;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
}
Here goes the main activity:
public class ShowCarExpList extends Activity {
private static final int CODE_CHANGE_DETAILS = 1;
private ShowCarExpandListAdapter expAdapter;
private ArrayList<ShowCarExpandGroup> expListItems;
private ExpandableListView expandList;
private Button back;
private Button edit;
private Button book;
private TextView brand;
private ImageView image;
private Car car;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("hemendik pasetan da", "egixe da?");
setContentView(R.layout.car);
setUpViews();
expandList = (ExpandableListView) findViewById (R.id.expCarList);
expListItems = setStandardGroups();
expAdapter = new ShowCarExpandListAdapter (ShowCarExpList.this, expListItems, car);
expandList.setAdapter(expAdapter);
// Set up our adapter
}
private void setUpViews() {
// TODO Auto-generated method stub
back = (Button)findViewById (R.id.btnBackCarView);
edit = (Button)findViewById (R.id.btnEditCarView);
book = (Button)findViewById (R.id.btnBookCarCarView);
Intent i = getIntent();
car = (Car) i.getParcelableExtra("car");
brand = (TextView)findViewById (R.id.txtCarViewBrand);
image = (ImageView)findViewById (R.id.ivCarMainPhoto);
String urlString = "http://tapazz.com/autopia/upload/cars/"+car.getPhotoUrl();
DrawableManager.fetchDrawableOnThread(urlString, image, null);
brand.setText(car.getBrand()+" "+car.getModel());
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(ShowCarExpList.this, MyCarsFragment.class);
ShowCarExpList.this.startActivity(in);
}
});
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(ShowCarExpList.this, AddCarActivity.class);
intent.putExtra("car", car);
startActivityForResult(intent, CODE_CHANGE_DETAILS);
}
});
}
public ArrayList<ShowCarExpandGroup> setStandardGroups(){
ArrayList<ShowCarExpandGroup> list = new ArrayList<ShowCarExpandGroup>();
ArrayList<ShowCarExpandChild> list2;
ArrayList<ShowCarMapChild> listMap = new ArrayList<ShowCarMapChild>();
ShowCarExpandGroup gru1 = new ShowCarExpandGroup();
gru1.setName(" Location");
ShowCarMapChild ch1_1 = new ShowCarMapChild();
ch1_1.setCar(car);
ch1_1.setTag(null);
listMap.add(ch1_1);
gru1.setMapItems(listMap);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru2 = new ShowCarExpandGroup();
gru2.setName(" License Plate");
ShowCarExpandChild ch2_1 = new ShowCarExpandChild();
ch2_1.setName(car.getLicensePlate());
ch2_1.setTag(null);
list2.add(ch2_1);
gru2.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru3 = new ShowCarExpandGroup();
gru3.setName(" Description");
ShowCarExpandChild ch3_1 = new ShowCarExpandChild();
if (car.getDescription()==null){
ch3_1.setName("Sorry no description available");
}else{
ch3_1.setName(car.getDescription());
}
ch3_1.setTag(null);
list2.add(ch3_1);
gru3.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru4 = new ShowCarExpandGroup();
gru4.setName(" Pricing");
ShowCarExpandChild ch4_1 = new ShowCarExpandChild();
ch4_1.setName("Price per hour: "+((Double)car.getPricePerHour()).toString());
ch4_1.setTag(null);
list2.add(ch4_1);
ShowCarExpandChild ch4_2 = new ShowCarExpandChild();
ch4_2.setName("Price per km: "+((Double)car.getPricePerKm()).toString());
ch4_2.setTag(null);
list2.add(ch4_2);
gru4.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru5 = new ShowCarExpandGroup();
gru5.setName(" Host/Owner");
ShowCarExpandChild ch5_1 = new ShowCarExpandChild();
ch5_1.setName("Maarten");
ch5_1.setTag(null);
list2.add(ch5_1);
gru5.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru6 = new ShowCarExpandGroup();
gru6.setName(" Reviews");
setReviews();
ShowCarExpandChild ch6_1 = new ShowCarExpandChild();
ch6_1.setName("no reviews");
ch6_1.setTag(null);
list2.add(ch6_1);
gru6.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru7 = new ShowCarExpandGroup();
gru7.setName(" Insurance");
ShowCarExpandChild ch7_1 = new ShowCarExpandChild();
ch7_1.setName(car.getInsuranceCompany());
ch7_1.setTag(null);
list2.add(ch7_1);
gru7.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru8 = new ShowCarExpandGroup();
gru8.setName(" More Pictures");
ShowCarExpandChild ch8_1 = new ShowCarExpandChild();
ch8_1.setName("no pictures");
ch8_1.setTag(null);
list2.add(ch8_1);
gru8.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru9 = new ShowCarExpandGroup();
gru9.setName(" Availability");
ShowCarExpandChild ch9_1 = new ShowCarExpandChild();
ch9_1.setName("now available");
ch9_1.setTag(null);
list2.add(ch9_1);
gru9.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru10 = new ShowCarExpandGroup();
gru10.setName(" Sharing settings");
ShowCarExpandChild ch10_1 = new ShowCarExpandChild();
ch10_1.setName("private");
ch10_1.setTag(null);
list2.add(ch10_1);
gru10.setItems(list2);
list2 = new ArrayList<ShowCarExpandChild>();
ShowCarExpandGroup gru11 = new ShowCarExpandGroup();
gru11.setName(" Keyless lock");
ShowCarExpandChild ch11_1 = new ShowCarExpandChild();
ch11_1.setName("Keylock installed! :)");
ch11_1.setTag(null);
list2.add(ch11_1);
gru11.setItems(list2);
list.add(gru1);
list.add(gru2);
list.add(gru3);
list.add(gru4);
list.add(gru5);
list.add(gru6);
list.add(gru7);
list.add(gru8);
list.add(gru9);
list.add(gru10);
list.add(gru11);
return list;
}
private void setReviews() {
// TODO Auto-generated method stub
}
}
and the error that it gives:
10-24 16:09:48.218: E/AndroidRuntime(353): FATAL EXCEPTION: main
10-24 16:09:48.218: E/AndroidRuntime(353): android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.maps.MapView
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-24 16:09:48.218: E/AndroidRuntime(353): at com.tapazz.car.ShowCarMapChild.getMapView(ShowCarMapChild.java:44)
10-24 16:09:48.218: E/AndroidRuntime(353): at com.tapazz.car.ShowCarExpandListAdapter.getChildView(ShowCarExpandListAdapter.java:80)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.ListView.makeAndAddView(ListView.java:1727)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.ListView.fillDown(ListView.java:652)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.ListView.fillSpecific(ListView.java:1284)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.ListView.layoutChildren(ListView.java:1558)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.View.layout(View.java:7035)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.View.layout(View.java:7035)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.View.layout(View.java:7035)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.ScrollView.onLayout(ScrollView.java:1205)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.View.layout(View.java:7035)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.View.layout(View.java:7035)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.View.layout(View.java:7035)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.os.Looper.loop(Looper.java:123)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-24 16:09:48.218: E/AndroidRuntime(353): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 16:09:48.218: E/AndroidRuntime(353): at java.lang.reflect.Method.invoke(Method.java:521)
10-24 16:09:48.218: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-24 16:09:48.218: E/AndroidRuntime(353): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-24 16:09:48.218: E/AndroidRuntime(353): at dalvik.system.NativeStart.main(Native Method)
10-24 16:09:48.218: E/AndroidRuntime(353): Caused by: java.lang.reflect.InvocationTargetException
10-24 16:09:48.218: E/AndroidRuntime(353): at com.google.android.maps.MapView.<init>(MapView.java:238)
10-24 16:09:48.218: E/AndroidRuntime(353): at java.lang.reflect.Constructor.constructNative(Native Method)
10-24 16:09:48.218: E/AndroidRuntime(353): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
10-24 16:09:48.218: E/AndroidRuntime(353): at android.view.LayoutInflater.createView(LayoutInflater.java:500)
10-24 16:09:48.218: E/AndroidRuntime(353): ... 38 more
10-24 16:09:48.218: E/AndroidRuntime(353): Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
10-24 16:09:48.218: E/AndroidRuntime(353): at com.google.android.maps.MapView.<init>(MapView.java:282)
10-24 16:09:48.218: E/AndroidRuntime(353): at com.google.android.maps.MapView.<init>(MapView.java:255)
10-24 16:09:48.218: E/AndroidRuntime(353): ... 42 more
Hope it will be helpfull
回答1:
You simply can't put a mapview inside an adapter (at least not in a seperate class). Mapview is not a view, mapview is a kind of activity/view that is combined and can't be split apart. I'm not sure what you are trying to achieve but if you could explain I might be able to suggest an alternative approach.
回答2:
As per official java doc only one map view can be used per map MapActivity. Following link also discuss the same issue:
How to to make 2 MapView on one Activity?
So to solve you problem one possible solution is to use the google static map API and open the full mapView when user clicks on satic map view. check out the answer from @Siddharth Lele in the following question for help on how to add static map...
Android - MapView contained within a Listview
you can use something like smart image view that allows setting http URI as image URI. you can set the static map URI as the Image URI to show the map in a ImageVIew. Source code of smart image view is also available at following location:
https://github.com/loopj/android-smart-image-view
Or may be you can redesign you app a little and create a separate map view and create multiple overlay to show you data.
来源:https://stackoverflow.com/questions/13048207/error-trying-to-add-a-map-android-view-inflateexception-binary-xml-file-line