4

嗨,我对 Android 有点陌生,我正在开发一个代码来显示和映射一些标记。我使用了 2 个按钮,一个用于查看地图,另一个用于添加信息。问题是我第一次点击显示地图时,它会显示,但是当我回到“主菜单”并再次点击显示地图时,只是出现黑屏并显示此错误:

android.view.inflateException: Binary XML file line #11: Error inflating class fragment

我正在使用 API 16,如果我没记错的话,我应该像现在一样使用 MapFragmet,而不是扩展 Fragment,因为它在旧版本中使用,不是吗?

我的 XML:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"  />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignRight="@+id/map"
        android:layout_alignTop="@+id/map"
        android:contentDescription="@string/hintlogo"
        android:src="@drawable/logo" />

</RelativeLayout>

我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="uri.br.rs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

<!-- Permissão para exibir o mapa -->
    <permission
        android:name="uri.br.rs.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="uri.br.rs.permission.MAPS_RECEIVE"/>

<!-- Permissão para interner, entre outros -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<!-- Estas permissões não são necessárias para usar o
         Google Maps Android API v2, mas são recomendadas. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<!-- Permissão para usar o OpenGL -->
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>    

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

<!-- Chave do Google Maps Android, registrada no site do Google APIs -->
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="some_key"/>


        <activity
            android:name="uri.br.rs.TudoImoveis"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>

我的班级名为 TudoImoveis:

package uri.br.rs;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class TudoImoveis extends Activity {

    String login = "admin", senha= "admin", loginaux, senhaaux;
    int guardaId;
// Dados do banco
    SQLiteDatabase bancoDados = null;
    Cursor cursor;
// Dados do mapa
    GoogleMap map;
    final LatLng stgo = new LatLng(-29.191357,-54.866588);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getApplicationContext(), "On Created!", Toast.LENGTH_LONG).show();
        menuPrincipal(); //chama menu inicial
    }

    @Override
      public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.tudo_imoveis, menu);
        return true;
      }

// ---------------------------------------------- Chama a tela inicial ----------------------------------------------
    public void menuPrincipal(){
        setContentView(R.layout.principal);

        Button btFechar = (Button) findViewById(R.id.btfechar);
        Button btVer = (Button) findViewById(R.id.btVer);
        Button btAnunciar = (Button) findViewById(R.id.btAnunciar);
        Button btLogar = (Button) findViewById(R.id.btLogar);
        TextView tvlogin = (TextView) findViewById(R.id.tvLogin);
        TextView tvsenha = (TextView) findViewById(R.id.tvSenha);
        EditText etLogin = (EditText) findViewById(R.id.etLogin);
        EditText etSenha = (EditText) findViewById(R.id.etSenha);
// Deixa login invisível
        tvlogin.setVisibility(View.INVISIBLE);
        tvsenha.setVisibility(View.INVISIBLE);
        etLogin.setVisibility(View.INVISIBLE);
        etSenha.setVisibility(View.INVISIBLE);
        btLogar.setVisibility(View.INVISIBLE);
// Botão ver
        btVer.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                abreOuCriaBanco();
                mostraMapa();
                Toast.makeText(getApplicationContext(), "Mapa", Toast.LENGTH_SHORT).show();
            }
        });
// Botão Anunciar
        btAnunciar.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                abreOuCriaBanco();
                logar();                
            }
        });
// Botão fechar
            btFechar.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    sairSistema();
                }
            });
    }

// ------------------------------------------- Função mostraMapa() ---------------------------------------------------
    public void mostraMapa(){
        try{
        setContentView(R.layout.mapa);

        ImageView logo = (ImageView) findViewById(R.id.logo);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        // utiliza-se este método para versões superiores ao Android 3.0 com import da biblioteca
        // import com.google.android.gms.maps.MapFragment;
        CameraUpdate update = CameraUpdateFactory.newLatLngZoom(stgo, 15);
        map.animateCamera(update);


// Ao clicar na imagem
        logo.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                menuPrincipal();
                //bancoDados.close();
                Toast.makeText(getApplicationContext(), "Menu Principal", Toast.LENGTH_SHORT).show();
            }
        });


// Se não tem dados carrega a casa
            if (buscaDados()==true){
                map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                map.addMarker(new MarkerOptions()
                            .position(new LatLng(-29.187648,-54.864245))
                                .title("Duque de Caxias, 513, apt. 403")
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.casa)));
            }
// Se tem dados 1º de maneira estática pra ver se funciona
            if (buscaDados()==false){
                map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
                map.addMarker(new MarkerOptions()
                                .position(new LatLng(-29.187648,-54.864245))
                                .title("Falso, 513, apt. 403")
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.casa)));
                Toast.makeText(getApplicationContext(), "Não existem registros!",Toast.LENGTH_SHORT).show();
            }

        }catch (Exception e){Toast.makeText(getApplicationContext(), "Erro: "+e, Toast.LENGTH_SHORT).show();}

    }

4

1 回答 1

1

将您的课程扩展到 Fragmentactivity

检查是否正确添加了谷歌播放服务以构建路径

转到项目|属性选择“订购和导出”选项卡中的 Java 构建路径部分选择 googleplay 服务并清理您的项目

于 2013-11-07T02:40:09.297 回答