setContentView() giving error - android

setContentView() is giving error in below is my code :
public class LocateUserInMap extends FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locate_user_in_map);
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
final LatLng CIU = new LatLng(19.111693900000000000,72.911271500000000000);
mMap.addMarker(new MarkerOptions().position(CIU).title("Friend"));
// findDirections(19.111693900000000000,72.911271500000000000,19.128203900000000000,72.928065000000060000, GMapV2Direction.MODE_DRIVING);
}
}
error :
here is my library that I am using :
and activity_locate_user_in_map.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=".LocateUserInMap" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>
what I am missing...
Please help!!!
Regards,
Sourabh

Your layout file R.layout.activity_locate_user_in_map is missing, maybe it is on another project or it is in correct project but somehow it can't be resolved, try to clean the project

The #SuppressLint("NewApi") really shouldnt be needed for onCreate() so I am going to guess that you have set your project to the something like:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
And then are trying to use Fragments without using the support library.
You will need to change the imports to something like:
import android.support.v4.app.FragmentActivity;
And change any calls to:
getFragmentManager()
to
getSupportFragmentManager()
You might also like to checkout this question for a good background on Fragment, FragmentActivity and the support lib

Related

GoogleMap complains of being a null object reference

I am working on an android application where I use maps and this is the error that I am getting
Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMapType(int)' on a null object reference
Prior to posting my query here I have looked for other similar issues but my code already have all what is being suggested so I was hoping if someone could point out the error.
This is the line which throws the error(second line).
I have no idea why it is caling map null when I've assigned it (at least I think I have)
private void initViews() {
map = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
//map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
and also this is my xml
<?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="vertical"
android:paddingTop="65dp" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Use getMapAsync(...) instead of getMap().
Example can be found here.

youtube android api error render on android studio

I program an app android to view youtube . I have a problem with android studio. I was copy YouTubeAndroidPlayerApi.jar to folder libs and code in java is fine , but in the xml is has error
Rendering Problems The following classes could not be found com.google.android.youtube.player.YouTubePlayerView (Fix Build Path, Create Class)
Tip: Try to build the project.
I was try to build project but it have the same error. Here is the code
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="5dp" />
Please help :D Tks all
I would recomend you adding the fragment programatically in your code like this:
YouTubePlayerSupportFragment videoStreamingPlayerFragment = YouTubePlayerSupportFragment.newInstance();
videoStreamingPlayerFragment.initialize(mDevKey, new PlayerInitializedListener());
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.Container, videoStreamingPlayerFragment);
ft.commitAllowingStateLoss();
in case you want to use the View instead of the fragment you could add it with the function addView:
YouTubePlayerView youtubeView = new YouTubePlayerView(this);
youtubeView.initialize(mDevKey, new PlayerInitializedListener());
FrameLayout youtubeContainer = (FrameLayout) findViewById(R.id.YoutubeContainer);
youtubeContainer.addView(mYoutubeView);
The activity where you do the operation must extend YouTubeBaseActivity

googlemap class in googlemapv2

i am currently working with googlemap v2. the problem is that i can't use the class "googlemap" it shows an error. i am using google api 4.2. how can i use this class. xml file code is given below
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
If you use
class="com.google.android.gms.maps.SupportMapFragment"
Then you have to use the SupportMapFragment class in your activities.
Example:
SupportMapFragment mapa;
mapa = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
Hope it helps
You need to reference GooglePlay services in your android project and import
import com.google.android.gms.maps.GoogleMap;
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap
You can follow
http://developer.android.com/google/play-services/setup.html
and Reference
Importing google-play-service library showing a red X next to this reference android

Google maps v2 on android devices with minSDK below 11

i have problem with this line when I create project which use Google maps API v2.
GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
It's says that I need to set minSDK to be minimum 11, but I want to use this application also on devices with android minSDK 8 (2.3.3). Any ideas? Or simply: How I can set google maps API v2 to be compatible with devices with minSDK 8.
Thanks
Use SupportMapFragment from a FragmentActivity, instead of MapFragment from an Activity. To use fragments on devices older than API Level 11, you need to use the Android Support package's backport of fragments (where FragmentActivity comes from).
http://android-er.blogspot.de/2012/12/using-supportmapfragment.html - small example
<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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
AND:
package de.meinprospekt.android.ui;
import java.text.SimpleDateFormat;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Fragment fragment = getSupportFragmentManager().findFragmentById(
R.id.map);
SupportMapFragment mapFragment = (SupportMapFragment) fragment;
GoogleMap mMap = mapFragment.getMap();

GoogleMap class object throw Nullpointer Exception

friends, I am getting NullPointer error ,on addMarker()
Below is my sample code.
GoogleMap myMap;
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment myMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
myMap.setMyLocationEnabled(true);
myMap.getUiSettings().setZoomControlsEnabled(true);
myMap.getUiSettings().setCompassEnabled(true);
myMap.getUiSettings().setMyLocationButtonEnabled(true);
Error message like NullPointer at line
myMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context=".DemoMapActiviy" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
any idea how can i solve it?
I went through this issue a while back. First check to see if your fragment is found by the fragment manager.
If not try replacing the way you've put your fragment into your view. If you want an example of another way I've posted a maps example here
Any more questions feel free to comment and I'll work through it with you :)
EDIT
have you got the correct API key in there? If you can't contact Google's servers then there's a good chance that that's your problem. Otherwise may be a manifest issue make sure you have READ_GSERVICES and INTERNET permissions.
See Google Maps API V2 'Failed to Load Map. Could not contact Google Servers'
Edit 2
Ok so I was being lazy before and just referring you to my project. If you were getting a connection to maps in your project lets start from there.
What I was suggesting in my original response was to check and see if myMapFragment was null. If it is great try doing something along the lines of the following:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SupportMapFragment myMapFragment = new SupportMapFragment();
ft.replace(R.id.map, myMapFragment, Constant.TAXI_FRAGMENT_ID);
ft.commit();
Where R.id.map is an empty FrameLayout
If myMapFragment isn't null then try extending SupportMapFragment and running the following test:
public class MyMapFragment extends SupportMapFragment {
#Override
public void onActivityCreated()
{
if (getMap() == null) Log.e("TAG", "There's something very wrong here";
}
}
Also if you do this make sure that you are inflating an instance of MyMapFragment not of SupportMapFragment

Categories

Resources