我尝试将LocationMapFragment实现到我的ContactUsActivity中,它在第29行的LocationMapFragment中显示错误。 我不知道如何初始化LocationMapFragment ...... 的错误:
05-29 12:10:37.305: E/AndroidRuntime(18087): FATAL EXCEPTION: main
05-29 12:10:37.305: E/AndroidRuntime(18087): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jumeirah.jumeirahmap/com.jumeirah.contactus.fragments.ContactUsActivity}: java.lang.NullPointerException
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.os.Looper.loop(Looper.java:137)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-29 12:10:37.305: E/AndroidRuntime(18087): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 12:10:37.305: E/AndroidRuntime(18087): at java.lang.reflect.Method.invoke(Method.java:511)
05-29 12:10:37.305: E/AndroidRuntime(18087): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 12:10:37.305: E/AndroidRuntime(18087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 12:10:37.305: E/AndroidRuntime(18087): at dalvik.system.NativeStart.main(Native Method)
05-29 12:10:37.305: E/AndroidRuntime(18087): Caused by: java.lang.NullPointerException
05-29 12:10:37.305: E/AndroidRuntime(18087): at com.jumeirah.contactus.fragments.LocationMapFragment.initilizeMap(LocationMapFragment.java:29)
05-29 12:10:37.305: E/AndroidRuntime(18087): at com.jumeirah.contactus.fragments.LocationMapFragment.onCreateView(LocationMapFragment.java:21)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.Activity.performStart(Activity.java:4475)
05-29 12:10:37.305: E/AndroidRuntime(18087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1940)
活动:
package com.jumeirah.contactus.fragments;
import com.jumeirah.jumeirahmap.R;
import com.jumeirah.jumeirahmap.R.id;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ContactUsActivity extends FragmentActivity{
private TextView propertyTextView, clubTextView, europeTextView,
mapTextView;
private View fragmentProperty,fragmentClub,fragmentEuropean;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.contactus_activity);
initViews();
}
private void initViews() {
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
PropertySalesFragment propertySalesFragment=new PropertySalesFragment();
fragmentTransaction.replace(R.id.contact_fragment_propertysales, propertySalesFragment);
ClubHouseFragment clubHouseFragment=new ClubHouseFragment();
fragmentTransaction.replace(R.id.contact_fragment_clubhouse, clubHouseFragment);
EuropeanTourFragment europeanTourFragment=new EuropeanTourFragment();
fragmentTransaction.replace(R.id.contact_fragment_europeantour, europeanTourFragment);
LocationMapFragment locationMapFragment=new LocationMapFragment();
fragmentTransaction.replace(R.id.contact_fragment_maps, locationMapFragment);
fragmentTransaction.commit();
propertyTextView = (TextView) findViewById(R.id.contact_text_property);
clubTextView = (TextView) findViewById(R.id.contact_text_club);
europeTextView = (TextView) findViewById(R.id.contact_text_european);
mapTextView = (TextView) findViewById(R.id.contact_text_map);
fragmentProperty=findViewById(R.id.contact_fragment_propertysales);
fragmentClub=findViewById(R.id.contact_fragment_clubhouse);
fragmentEuropean=findViewById(id.contact_fragment_europeantour);
fragmentProperty.setVisibility(View.GONE);
fragmentClub.setVisibility(View.GONE);
fragmentEuropean.setVisibility(View.GONE);
}
}
LocationMapFragment:
package com.jumeirah.contactus.fragments;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.jumeirah.jumeirahmap.R;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class LocationMapFragment extends Fragment{
private GoogleMap googleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
initilizeMap();
return inflater.inflate(R.layout.location_map_activity, container, false);
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
/* Alert.showAlert(LocationMapActivity.this,
getString(R.string.error_title),
getString(R.string.reg_err_choose_question),
getString(R.string.error_positive_btn), null, true);*/
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return null;
}
}
XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#B313C242" />
<TextView
android:id="@+id/contact_text_property"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#555555"
android:gravity="center_vertical|center"
android:paddingLeft="10dp"
android:text="@string/contact_property_sales"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#B313C242" />
<fragment
android:name="com.jumeirah.contactus.fragments.PropertySalesFragment"
android:id="@+id/contact_fragment_propertysales"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/contact_text_club"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#555555"
android:gravity="center_vertical|center"
android:paddingLeft="10dp"
android:text="@string/contact_club_house"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#B313C242" />
<fragment
android:name="com.jumeirah.contactus.fragments.ClubHouseFragment"
android:id="@+id/contact_fragment_clubhouse"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/contact_text_european"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#555555"
android:gravity="center_vertical|center"
android:paddingLeft="10dp"
android:text="@string/contact_european_tour_int"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#B313C242" />
<fragment
android:name="com.jumeirah.contactus.fragments.EuropeanTourFragment"
android:id="@+id/contact_fragment_europeantour"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/contact_text_map"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#555555"
android:gravity="center_vertical|center"
android:paddingLeft="10dp"
android:text="@string/contact_location_map"
android:textColor="#FFFFFF" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#B313C242" />
<fragment
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/contact_fragment_maps"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jumeirah.jumeirahmap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" >
</uses-feature>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jumeirah.contactus.fragments.ContactUsActivity"
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="AIzaSyCVC-ZaQ2yuDyF-nbqpVZr5tzj8D_Wff0o" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
我的location_map_activity.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"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
答案 0 :(得分:0)
试试这个..
改变这个..
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
到
googleMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
答案 1 :(得分:0)
Hey Arun问题在
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
initilizeMap();
return inflater.inflate(R.layout.location_map_activity, container, false);
}
这里是initilizeMap();在xml寄存器之前调用,所以只需将其调用到onActivityCreated(); 而已... 或第二种方式
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.location_map_activity, container,false);
initilizeMap();
return view;
}
多数民众赞成......