我搜索了很多时间我的问题的一些答案,但我开始编写一段时间,我的代码没有成功。所以我请求你的帮助。非常感谢!
因此,当我运行我的程序时它工作(没有项目错误)但在我的模拟器上它不起作用,我在logcat中有这个错误
我的logcat
*05-08 04:53:04.448: E/AndroidRuntime(1057): FATAL EXCEPTION: main
05-08 04:53:04.448: E/AndroidRuntime(1057): Process: com.example.carte, PID: 1057
05-08 04:53:04.448: E/AndroidRuntime(1057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carte/com.example.carte.MainActivity}: java.lang.NullPointerException
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.os.Looper.loop(Looper.java:136)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-08 04:53:04.448: E/AndroidRuntime(1057): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 04:53:04.448: E/AndroidRuntime(1057): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 04:53:04.448: E/AndroidRuntime(1057): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-08 04:53:04.448: E/AndroidRuntime(1057): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-08 04:53:04.448: E/AndroidRuntime(1057): at dalvik.system.NativeStart.main(Native Method)
05-08 04:53:04.448: E/AndroidRuntime(1057): Caused by: java.lang.NullPointerException
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
05-08 04:53:04.448: E/AndroidRuntime(1057): at com.example.carte.MainActivity.onCreate(MainActivity.java:34)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.Activity.performCreate(Activity.java:5231)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-08 04:53:04.448: E/AndroidRuntime(1057): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)*
因为我说我试图找到解决方案:我添加了
<uses-library android:name="com.google.android.maps" />
在我的Android清单中,所有错误都消失了,但仍然存在错误
enter code here`"INSTALL_FAILED_MISSING_SHARED_LIBRARY".
我的MainActivity.java
package com.example.carte;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import com.example.carte.R;
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.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends ActionBarActivity {
static final LatLng HAMBURG = new LatLng(53.568, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.fragment_main);
super.onCreate(savedInstanceState);
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
if (map!=null){
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.carte"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.carte.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="A----------------------------" />
</application>
我添加了Google图书馆,并在Android SDK管理器中下载了所有Google附加内容。
我是法国人,所以我请你原谅我的英语......请原谅我的页面布局,这是我第一次尝试尽力而为。谢谢你的帮助
答案 0 :(得分:0)
请尝试按照本指南操作: https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2
对于Maps V2,同样重要的是签署项目:
http://developer.android.com/tools/publishing/app-signing.html
从我在这里快速阅读的内容看来,您应该使用密钥来签署项目,而不是在清单中。以下内容应该在您的清单中,而不是API密钥:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
答案 1 :(得分:0)
您应该尝试使用其他一些模拟器选项,例如
,而不是尝试测试您的应用程序https://cloud.genymotion.com/page/launchpad/download/ 和 http://www.bluestacks.com/
答案 2 :(得分:0)
不幸的是,对于使用Google地图,您必须在真实设备上测试该应用程序,因为不支持该模拟器。
如果您有关于此的详细信息,请阅读Google Maps Android API v2 - Tutorial