Android 在谷歌地图上搜索时出现空指针或膨胀异常
我是一名初学者,目前正在尝试使用GoogleMapsAPIv2+fragments+actionbar进行一项实验,但我认为我遇到了麻烦。该应用程序的用途非常简单,您可以从片段上的edittext或操作栏上的edittext键入位置,然后当您点击搜索时,应用程序会在想要的位置上显示带有标记的地图。如果键入搜索并按下按钮,则会出现以下错误:Android 在谷歌地图上搜索时出现空指针或膨胀异常,android,nullpointerexception,google-maps-android-api-2,inflate-exception,Android,Nullpointerexception,Google Maps Android Api 2,Inflate Exception,我是一名初学者,目前正在尝试使用GoogleMapsAPIv2+fragments+actionbar进行一项实验,但我认为我遇到了麻烦。该应用程序的用途非常简单,您可以从片段上的edittext或操作栏上的edittext键入位置,然后当您点击搜索时,应用程序会在想要的位置上显示带有标记的地图。如果键入搜索并按下按钮,则会出现以下错误: 01-10 17:29:06.560: E/AndroidRuntime(17425): FATAL EXCEPTION: main 01-10 17:29
01-10 17:29:06.560: E/AndroidRuntime(17425): FATAL EXCEPTION: main
01-10 17:29:06.560: E/AndroidRuntime(17425): java.lang.NullPointerException
01-10 17:29:06.560: E/AndroidRuntime(17425): at alan.android.testMapas.FragOne$1.onClick(FragOne.java:32)
01-10 17:29:06.560: E/AndroidRuntime(17425): at android.view.View.performClick(View.java:3574)
01-10 17:29:06.560: E/AndroidRuntime(17425): at android.view.View$PerformClick.run(View.java:14293)
01-10 17:29:06.560: E/AndroidRuntime(17425): at android.os.Handler.handleCallback(Handler.java:605)
01-10 17:29:06.560: E/AndroidRuntime(17425): at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 17:29:06.560: E/AndroidRuntime(17425): at android.os.Looper.loop(Looper.java:137)
01-10 17:29:06.560: E/AndroidRuntime(17425): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-10 17:29:06.560: E/AndroidRuntime(17425): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 17:29:06.560: E/AndroidRuntime(17425): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 17:29:06.560: E/AndroidRuntime(17425): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-10 17:29:06.560: E/AndroidRuntime(17425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-10 17:29:06.560: E/AndroidRuntime(17425): at dalvik.system.NativeStart.main(Native Method)
它仅在地图已显示的情况下有效,但如果我更改选项卡并再次返回或搜索,则会出现充气异常:
01-10 17:32:26.080: E/AndroidRuntime(17681): FATAL EXCEPTION: main
01-10 17:32:26.080: E/AndroidRuntime(17681): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-10 17:32:26.080: E/AndroidRuntime(17681): at alan.android.testMapas.FragSearch.onCreateView(FragSearch.java:35)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.os.Handler.handleCallback(Handler.java:605)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.os.Looper.loop(Looper.java:137)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-10 17:32:26.080: E/AndroidRuntime(17681): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 17:32:26.080: E/AndroidRuntime(17681): at java.lang.reflect.Method.invoke(Method.java:511)
01-10 17:32:26.080: E/AndroidRuntime(17681): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-10 17:32:26.080: E/AndroidRuntime(17681): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-10 17:32:26.080: E/AndroidRuntime(17681): at dalvik.system.NativeStart.main(Native Method)
01-10 17:32:26.080: E/AndroidRuntime(17681): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040036, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
01-10 17:32:26.080: E/AndroidRuntime(17681): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
01-10 17:32:26.080: E/AndroidRuntime(17681): ... 18 more
以下是主要活动:
public class MainActivity extends SherlockFragmentActivity {
public ActionBar bar;
private EditText txtsearch;
// private
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bar = getSupportActionBar();
//mTabSeeker=new TabSeeker(bar);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayShowTitleEnabled(false);
Tab tab0 = bar
.newTab()
.setText("1")
.setTabListener(
new SherlockTabListener<FragOne>(this, "One",
FragOne.class));
bar.addTab(tab0);
Tab tab1 = bar
.newTab()
.setText("2")
.setTabListener(
new SherlockTabListener<FragSearch>(this, "Search",
FragSearch.class));
bar.addTab(tab1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.activity_main, (Menu)menu);
View v = (View)menu.findItem(R.id.searchoption).getActionView();
txtsearch = (EditText)v.findViewById(R.id.txt_search);
txtsearch.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
FragSearch fs;
bar.setSelectedNavigationItem(1);
fs = (FragSearch) getSupportFragmentManager().findFragmentByTag("Search");
fs.locateSearch(txtsearch.getText().toString());
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
fragsearch.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
style="@style/Theme.Sherlock.Light.Dialog"
android:id="@+id/txt_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.67"
android:inputType="text" />
</LinearLayout>
和search.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
style="@style/Theme.Sherlock.Light.Dialog"
android:id="@+id/txt_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.67"
android:inputType="text" />
</LinearLayout>
提前谢谢大家!如果需要我从项目发布任何东西,请让我知道
Edit:我已经更新了这两个日志,我认为null指针是给定的,因为它必须在访问locateSearch方法之前实例化,所以我认为我必须找到一种方法,将文本作为参数传递,并在实例化后定位,但是对于InflateExceptioin,看起来我必须找到另一种方法来放大片段的视图,因为TableSistener是我在其他应用程序上使用的,没有关于常规片段的问题。有什么想法吗
java.lang.NullPointerException
at alan.android.testMapas.FragOne$1.onClick(FragOne.java:32)
对于第一组错误,按钮的OnClickListener中有一个空值。我猜它是fb
,因为我看不出你在哪里给了任何片段标签“Prestamo”
。但从LogCat所述的第32行开始
Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040036, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
发生第二组错误,是因为每次单击选项卡时都要创建现有片段的新副本。(因此出现重复id错误。)您的TablListener应该重新加载已经存在的片段。这里有一个简单的帮助
对于第一组错误,按钮的OnClickListener中有一个空值。我猜它是fb
,因为我看不出你在哪里给了任何片段标签“Prestamo”
。但从LogCat所述的第32行开始
Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f040036, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
发生第二组错误,是因为每次单击选项卡时都要创建现有片段的新副本。(因此出现重复id错误。)您的TablListener应该重新加载已经存在的片段。这里有一个简单的帮助。最后我排除了IllegalArgumentException。在做了一些研究之后,我最终改变了我的OnCreateView()并扩展了SupportMapFragment而不是SherlockFragment。代码如下:
public class FragSearch extends SupportMapFragment {
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
map = getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return view;
}
public void locateSearch(String text) {
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
try {
List<Address> list = geocoder.getFromLocationName(text, 1);
if (list != null) {
Double lat = (Double) list.get(0).getLatitude();
Double lng = (Double) list.get(0).getLongitude();
LatLng ubicacion = new LatLng(lat, lng);
CameraPosition camUpd1 = new CameraPosition.Builder()
.target(ubicacion).zoom(16).build();
CameraUpdate camUpd3 = CameraUpdateFactory
.newCameraPosition(camUpd1);
map.clear();
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng)).title(text));
map.animateCamera(camUpd3);
} else {
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Search Error", Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
}
}
公共类FragSearch扩展了SupportMapFragment{
私人谷歌地图;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=super.onCreateView(充气机、容器、保存状态);
map=getMap();
map.setMapType(GoogleMap.map\u TYPE\u NORMAL);
返回视图;
}
公共void locateSearch(字符串文本){
Geocoder Geocoder=新的地理编码器(getActivity(),Locale.getDefault());
试一试{
列表=geocoder.getFromLocationName(文本,1);
如果(列表!=null){
Double lat=(Double)list.get(0.getLatitude();
Double lng=(Double)list.get(0.getLongitude();
LatLng ubicacion=新LatLng(lat,lng);
CameraPosition camUpd1=新建CameraPosition.Builder()
.target(ubicacion).zoom(16.build();
CameraUpdate camUpd3=CameraUpdateFactory
.新摄像机位置(camUpd1);
map.clear();
addMarker(新的MarkerOptions()
职位(新LatLng(lat,lng))。标题(文本);
map.animateCamera(camUpd3);
}否则{
}
}捕获(例外e){
Toast.makeText(getActivity(),“搜索错误”,Toast.LENGTH\u SHORT)
.show();
e、 printStackTrace();
}
}
}
至于NPE,它似乎抛出了该异常,因为我试图在实例化该对象之前访问方法,所以我似乎必须尝试找到解决方法。最后我排除了IllegalArgumentException。在做了一些研究之后,我最终改变了我的OnCreateView()并扩展了SupportMapFragment而不是SherlockFragment。代码如下:
public class FragSearch extends SupportMapFragment {
private GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
map = getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
return view;
}
public void locateSearch(String text) {
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
try {
List<Address> list = geocoder.getFromLocationName(text, 1);
if (list != null) {
Double lat = (Double) list.get(0).getLatitude();
Double lng = (Double) list.get(0).getLongitude();
LatLng ubicacion = new LatLng(lat, lng);
CameraPosition camUpd1 = new CameraPosition.Builder()
.target(ubicacion).zoom(16).build();
CameraUpdate camUpd3 = CameraUpdateFactory
.newCameraPosition(camUpd1);
map.clear();
map.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng)).title(text));
map.animateCamera(camUpd3);
} else {
}
} catch (Exception e) {
Toast.makeText(getActivity(), "Search Error", Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
}
}
}
公共类FragSearch扩展了SupportMapFragment{
私人谷歌地图;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=super.onCreateView(充气机、容器、保存状态);
map=getMap();
map.setMapType(GoogleMap.map\u TYPE\u NORMAL);
返回视图;
}
公共void locateSearch(字符串文本){
Geocoder Geocoder=新的地理编码器(getActivity(),Locale.getDefault());
试一试{
列表=geocoder.getFromLocationName(文本,1);
如果(列表!=null){
Double lat=(Double)list.get(0.getLatitude();
Double lng=(Double)list.get(0.getLongitude();
LatLng ubicacion=新LatLng(lat,lng);
CameraPosition camUpd1=新建CameraPosition.Builder()
.target(ubicacion).zoom(16.build();
CameraUpdate camUpd3=CameraUpdateFactory
.新摄像机位置(camUpd1);
map.clear();
addMarker(新的MarkerOptions()
职位(新LatLng(lat,lng))。标题(文本);
map.animateCamera(camUpd3);
}否则{
}
}捕获(例外e){
Toast.makeText(getActivity(),“搜索错误”,Toast.LENGTH\u SHORT)
.show();