FATAL EXCEPTION: main java.lang.NullPointerException - exception

I'm getting a fatal exception / null pointer exception. I don´t know why
If any additional information is required please let me know.
Why i´m getting a Nullpointer Exception?
Please let me know, if you need more Informationens.
JAVA
public class MainActivity extends Activity {
private ImageButton car;
private ImageButton foot;
private ImageButton bus;
private ImageButton train;
private ImageButton carlift;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.startpic);
// display the logo during 5 secondes,
new CountDownTimer(5000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
car = (ImageButton) findViewById(R.id.imageButton1);
foot = (ImageButton) findViewById(R.id.imageButton2);
bus = (ImageButton) findViewById(R.id.imageButton3);
train = (ImageButton) findViewById(R.id.imageButton4);
carlift = (ImageButton) findViewById(R.id.imageButton5);
car.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.car);
}
});
foot.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.foot);
}
});
bus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.bus);
}
});
train.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.train);
}
});
carlift.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.carlift);
}
});
super.onCreate(savedInstanceState);
}
#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;
}
}
This is my Logcat
LOGCAT
03-11 12:46:22.637: E/AndroidRuntime(1769): FATAL EXCEPTION: main
03-11 12:46:22.637: E/AndroidRuntime(1769): Process: com.gunasiri.home, PID: 1769
03-11 12:46:22.637: E/AndroidRuntime(1769): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com.gunasiri.home/com.gunasiri.home.MainActivity}:
java.lang.NullPointerException
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.access$700(ActivityThread.java:135)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.os.Handler.dispatchMessage(Handler.java:102)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.os.Looper.loop(Looper.java:137)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-11 12:46:22.637: E/AndroidRuntime(1769): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 12:46:22.637: E/AndroidRuntime(1769): at java.lang.reflect.Method.invoke(Method.java:515)
03-11 12:46:22.637: E/AndroidRuntime(1769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-11 12:46:22.637: E/AndroidRuntime(1769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-11 12:46:22.637: E/AndroidRuntime(1769): at dalvik.system.NativeStart.main(Native Method)
03-11 12:46:22.637: E/AndroidRuntime(1769): Caused by: java.lang.NullPointerException
03-11 12:46:22.637: E/AndroidRuntime(1769): at com.gunasiri.home.MainActivity.onCreate(MainActivity.java:39)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.Activity.performCreate(Activity.java:5243)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-11 12:46:22.637: E/AndroidRuntime(1769): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
03-11 12:46:22.637: E/AndroidRuntime(1769): ... 11 more

The ImageButtons are all null now, because the correct content view isn't set yet. You need to set the OnClickListeners in the onFinish() method.
So your code would be:
public class MainActivity extends Activity {
private ImageButton car;
private ImageButton foot;
private ImageButton bus;
private ImageButton train;
private ImageButton carlift;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.startpic);
// display the logo during 5 secondes,
new CountDownTimer(5000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
// set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
car = (ImageButton) findViewById(R.id.imageButton1);
foot = (ImageButton) findViewById(R.id.imageButton2);
bus = (ImageButton) findViewById(R.id.imageButton3);
train = (ImageButton) findViewById(R.id.imageButton4);
carlift = (ImageButton) findViewById(R.id.imageButton5);
car.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.car);
}
});
foot.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.foot);
}
});
bus.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.bus);
}
});
train.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.train);
}
});
carlift.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.carlift);
}
});
}
}.start();
super.onCreate(savedInstanceState);
}
#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;
}
}

Related

how to add ( admob ) Interstitial ads to libgdx game and what activity to use?

I followed google guide:
updated build.gradle dependencies
updated AndroidManifest.xml
updated the AndroidLauncher and tried banner ads first
from libgdx wiki https://libgdx.com/wiki/third-party/admob-in-libgdx
#Override public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libGDX View
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new mygame(), config);
// Create and setup the AdMob view
AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Put in your secret key here
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
// Add the libGDX view
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}}
but I cant figure out how to do the same for Interstitial ads
i tried adding adscontroller interface
public interface AdsController {
public void loadInterstitialAd();
public void showInterstitialAd();
}
and updating AndroidLauncher
public class AndroidLauncher extends AndroidApplication implements AdsController {
InterstitialAd mInterstitialAd;
private static final String TAG = "Androidlauncher";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// // Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libGDX View
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new mygame(this), config);
layout.addView(gameView);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
AdRequest adRequest = null;
InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
#Override
public void onAdLoaded(#NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i(TAG, "onAdLoaded");
}
#Override
public void onAdFailedToLoad(#NonNull LoadAdError loadAdError) {
// Handle the error
Log.d(TAG, loadAdError.toString());
mInterstitialAd = null;
}
});
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
#Override
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.");
}
#Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
// Set the ad reference to null so you don't show the ad a second time.
Log.d(TAG, "Ad dismissed fullscreen content.");
mInterstitialAd = null;
}
#Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
mInterstitialAd = null;
}
#Override
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.");
}
#Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.");
}
});
loadInterstitialAd();
}
#Override
public void loadInterstitialAd() {
AdRequest adRequest = new AdRequest.Builder().build();
}
#Override
public void showInterstitialAd() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if(mInterstitialAd!=null) {
mInterstitialAd.show();
}
else loadInterstitialAd();
}
});
}
}
InterstitialAd.show(MyActivity.this); require activity but libgdx doesn't work like that(I think?)
every code I found is no longer useful because google updated Admob
AndroidApplication extends Activity, so for interstitials you can just pass in a reference to the application, eg InterstitialAd.show(this);
I did something very similar to get interstitials working in my project. I use Ironsource but the process should be very similar. First, I defined an AdManager interface:
public interface AdManager {
/**
* Show a rewarded video ad
*/
void showRewardedVideo();
/**
* Called on app pause
*/
void onPause();
/**
* Called on app resume
*/
void onResume();
/**
* Attempts to show an interstitial ad
*
* #param onSuccess
* #param onFailed
*/
void showInterstitial(Listener onSuccess, Listener onFailed);
/**
* Called every frame, for any extra work that might need to be done
*
* #param deltaTime
*/
void update(float deltaTime);
}
Following that, you can implement your platform's ad provider:
public class AndroidAdManager implements AdManager, RewardedVideoListener, InterstitialListener, OfferwallListener {
private OnlineRPG game;
private boolean videoAvailable;
private Listener onInterstitialSuccess;
private Listener onInterstitialFailed;
private float timeSinceAd;
public AndroidAdManager(Activity activity, Gamegame) {
this.game = game;
this.activity = activity;
IronSource.setRewardedVideoListener(this);
IronSource.setInterstitialListener(this);
IronSource.setOfferwallListener(this);
IronSource.init(activity, "whatever");
IronSource.shouldTrackNetworkState(activity, true);
IronSource.loadInterstitial();
IntegrationHelper.validateIntegration(activity);
}
#Override
public void showRewardedVideo() {
if (IronSource.isRewardedVideoPlacementCapped(REWARDED_VIDEO_PLACEMENT_NAME)) {
Log.i(TAG, "Rewarded video placement is capped");
return;
}
IronSource.showRewardedVideo(REWARDED_VIDEO_PLACEMENT_NAME);
}
#Override
public void onPause() {
IronSource.onPause(activity);
}
#Override
public void onResume() {
IronSource.onResume(activity);
}
#Override
public void showInterstitial(Listener onSuccess, Listener onFailed) {
if (timeSinceAd < INTERSTITIAL_MIN_PERIOD || true) {
onFailed.invoke();
return;
}
this.onInterstitialSuccess = onSuccess;
this.onInterstitialFailed = onFailed;
IronSource.showInterstitial(INTERSTITIAL_PLACEMENT_NAME);
}
#Override
public void update(float deltaTime) {
timeSinceAd += deltaTime;
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoAdClosed() {
}
#Override
public void onRewardedVideoAvailabilityChanged(boolean b) {
Log.i(TAG, "onRewardedVideoAvailabilityChanged: " + b);
videoAvailable = b;
}
#Override
public void onRewardedVideoAdStarted() {
}
#Override
public void onRewardedVideoAdEnded() {
}
#Override
public void onRewardedVideoAdRewarded(Placement placement) {
}
#Override
public void onRewardedVideoAdShowFailed(IronSourceError ironSourceError) {
}
#Override
public void onRewardedVideoAdClicked(Placement placement) {
}
#Override
public void onInterstitialAdReady() {
}
#Override
public void onInterstitialAdLoadFailed(IronSourceError ironSourceError) {
if (onInterstitialFailed != null) {
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
onInterstitialFailed.invoke();
onInterstitialFailed = null;
}
});
}
}
#Override
public void onInterstitialAdOpened() {
Log.i(TAG, "Interstitial Ad Opened");
}
#Override
public void onInterstitialAdClosed() {
Log.i(TAG, "Interstitial Ad Closed");
if (onInterstitialSuccess != null) {
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
timeSinceAd = 0;
onInterstitialSuccess.invoke();
onInterstitialSuccess = null;
}
});
}
IronSource.loadInterstitial();
}
#Override
public void onInterstitialAdShowSucceeded() {
}
#Override
public void onInterstitialAdShowFailed(IronSourceError ironSourceError) {
Log.e(TAG, ironSourceError.getErrorMessage());
if (onInterstitialFailed != null) {
Gdx.app.postRunnable(new Runnable() {
#Override
public void run() {
onInterstitialFailed.invoke();
onInterstitialFailed = null;
}
});
}
}
#Override
public void onInterstitialAdClicked() {
}
#Override
public void onOfferwallAvailable(boolean b) {
}
#Override
public void onOfferwallOpened() {
}
#Override
public void onOfferwallShowFailed(IronSourceError ironSourceError) {
}
#Override
public boolean onOfferwallAdCredited(int i, int i1, boolean b) {
return false;
}
#Override
public void onGetOfferwallCreditsFailed(IronSourceError ironSourceError) {
}
#Override
public void onOfferwallClosed() {
}
}
Lastly, in your AndroidLauncher you can create your AndroidAdManager, giving it a reference to your game/activity.
public class AndroidLauncher extends AndroidApplication {
private Game game;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
game = new Game();
game.setAdManager(new AndroidAdManager(this, game));
game.setPermissionManager(new AndroidPermissionManager(this, game));
initialize(game, config);
}
#Override
protected void onPause() {
super.onPause();
game.getAds().onPause();
}
#Override
protected void onResume() {
super.onResume();
game.getAds().onResume();
}
}
I hope this helps in your project!

Getting data in recyclerView

i am getting messages from the database into a recycler view but i want to refresh the recycler view every second for to adding different for to getting new messages in recycler view
But i haver an error of getting data please see it https://www.filemail.com/d/izhittgyibvcjxx
my code is
boolean refresh;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_user);
refresh = true;
content();
}
public void content()
{
getdata();
if (refresh)
{
refresh(100);
}
}
private void refresh(int milliseconds)
{
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
#Override
public void run() {
content();
}
};
handler.postDelayed(runnable,milliseconds);
}
private void getdata()
{
String Choice = "Get Messages";
Call<List<responsemodel>> call = SplashScreen.apiInterface.getfullprofiledata(Choice,Message_To,Message_From);
call.enqueue(new Callback<List<responsemodel>>() {
#Override
public void onResponse(Call<List<responsemodel>> call, Response<List<responsemodel>> response) {
List<responsemodel> data = response.body();
Message_user_Adapter adapter = new Message_user_Adapter(data,Message_To);
messages_Message_user_RecyclerView.setAdapter(adapter);
messages_Message_user_RecyclerView.smoothScrollToPosition(messages_Message_user_RecyclerView.getAdapter().getItemCount());
}
#Override
public void onFailure(Call<List<responsemodel>> call, Throwable t) {
}
});
}

reopen Google map stops my Application

i have tow fragment. by click in first fragment button second fragment that is Google map will open.
first time all thing is correct but if i back and click again program will stop.
map fragment code is as blow:
public class map extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
private double lat;
private double att;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.map,container,false);
// GoogleMap map = ((MapFragment) getFragmentManager(.findFragmentById(R.id.map)).getMap();
return view;
}
#Override
public void onStart() {
super.onStart();
if (getArguments() != null) {
lat= getArguments().getDouble("LAT");
att=getArguments().getDouble("ATT");
Toast.makeText(getActivity(),String.valueOf(att),Toast.LENGTH_LONG).show();
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
}
#Override
public void onMapReady(GoogleMap googleMap) {
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(lat, att);
CameraPosition googlePlex = CameraPosition.builder()
.target(new LatLng(lat,att))
.zoom(16).build();
googleMap.addMarker(new MarkerOptions().position(sydney));
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));
}
}
and main activity is:
public class MainActivity extends AppCompatActivity implements onclicklistener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.firstpage)!=null)
{
if(savedInstanceState!=null)
return;
firstpage startpage=new firstpage();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.add(R.id.firstpage,startpage);
ft.commit();
}
}
#Override
public void onclickbutton(String index) {
try{ map newmap=new map();
Bundle args = new Bundle();
args.putDouble("LAT",32.657892);
args.putDouble("ATT",51.668643);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
newmap.setArguments(args);
ft.addToBackStack(null);
ft.replace(R.id.firstpage,newmap);
ft.commit();}
catch (Exception e){Toast toast = Toast.makeText(this, "error", Toast.LENGTH_SHORT);}
}
#Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0 ){
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
first Run is completely OK but if i click again program will stop. please guide me.
it solved.
i added this code:
#Override
public void onDetach() {
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
super.onDetach();
}

Using google maps via navigation drawer works fine when application starts but reclicking on the tab generates illegal exception error

If I replace the fragment, where the map fragment is in (in this code example MyFragment) with a different fragment and then come back, I get an
IllegalStateException
public class Home_Map extends Fragment {
GoogleMap googleMap;
FragmentManager myFragmentManager;
SupportMapFragment mySupportMapFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_home__map, container, false);
//googleMap.setMyLocationEnabled(true);
try {
// Loading map
initilizeMap();
googleMap.setMyLocationEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
private void initilizeMap() {
try
{
if (googleMap == null) {
myFragmentManager = getFragmentManager();
mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map2);
googleMap = mySupportMapFragment.getMap();
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
} catch (Exception e) { Toast.makeText(getActivity().getApplicationContext(), ""+e, 1).show();
// TODO: handle exception
}
}
#Override
public void onResume() {
super.onResume();
initilizeMap();
}
#Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
You need to release the memory when changing.
Try to use following code:
public class Home_Map extends Fragment {
private MapView mapView;
public static MapsFragment newInstance() {
MapsFragment fragment = new MapsFragment();
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.mapview, container, false);
// Gets the MapView from the XML layout and creates it
MapsInitializer.initialize(getActivity());
switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity())) {
case ConnectionResult.SUCCESS:
Toast.makeText(getActivity(), "SUCCESS", Toast.LENGTH_SHORT).show();
mapView = (MapView) v.findViewById(R.id.map);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
if (mapView != null) {
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
googleMap.setMyLocationEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
googleMap.animateCamera(cameraUpdate);
}
});
}
break;
case ConnectionResult.SERVICE_MISSING:
Toast.makeText(getActivity(), "SERVICE MISSING", Toast.LENGTH_SHORT).show();
break;
case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
Toast.makeText(getActivity(), "UPDATE REQUIRED", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getActivity(), GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()), Toast.LENGTH_SHORT).show();
}
return v;
}
#Override
public void onResume() {
mapView.onResume();
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
And the layout xml file is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name=".MapsFragment"/>
</RelativeLayout>
Note that you should import android.support.v4.app.Fragment; on both your DrawerLayout file andHome_Map.
Finally use the
fragment = MapsFragment.newInstance();
instead of
fragment = new Home_Map();
in your DrawerLayout.
You can also try my project here.

How Can I log exception stacktrace via custom junit runner?

Hi I have custom junit runner
public class InterceptorRunner extends BlockJUnit4ClassRunner {
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface InterceptorClasses {
public Class<?>[] value();
}
public InterceptorRunner(Class<?> klass) throws InitializationError {
super(klass);
}
#Override
public Statement methodInvoker(FrameworkMethod method, Object test) {
InterceptorStatement statement = new InterceptorStatement(super.methodInvoker(method, test));
InterceptorClasses annotation = test.getClass().getAnnotation(InterceptorClasses.class);
Class<?>[] klasez = annotation.value();
try {
for (Class<?> klaz : klasez) {
statement.addInterceptor((Interceptor) klaz.newInstance());
}
} catch (IllegalAccessException ilex) {
ilex.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return statement;
}
#Override
public void run(RunNotifier notifier) {
FailListener listener = new FailListener();
notifier.addListener(listener);
super.run(notifier);
notifier.removeListener(listener);
}
}
and custom listener
public class FailListener extends RunListener {
#Override
public void testFailure(Failure failure) throws Exception {
System.out.println("test fails");
super.testFailure(failure);
}
public void testStarted(Description description) throws Exception {
System.out.println("Test started");
super.testStarted(description);
}
}
How can I log not only System.out.println("test fails"); but also Exception and some other information?
It seems to me that it possible to use failure, but I don't know how to.
The Failure object has a method getException().