Google maps app inflation exception - google-maps

I keep getting errors while accessing the maps part of my app
I included the google play services dependency in my app is there any other library i need to include in it? How do I add the library? All tutorials on the internet use older verions of android studio while I use the latest one.
enter code here
FATAL EXCEPTION: main
"java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.zeda.TaxiTown/com.zeda.TaxiTown.map}:
android.view.InflateException: Binary XML file line #18: Error inflating class fragment"
in my android maps application what does this mean?
XML
<?xml version="1.0" encoding="utf-8"?>
<!-- This can go anywhere in your layout (see other demos for some examples). -->
<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"/>
JAVA file has this in it
package com.zeda.TaxiTown;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class map extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, 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);
}
}

I switched to using MapView instead and it worked!

Related

How to fix inconvertible types in Android Studio

I am trying to start an application based on google maps in Android Studio. I followed the instructions and started an Google Maps activity.
Then I created according to the instructions an API key and enabled the maps SDK android for android and copied the key into the app.
but then when I'm trying to run the app (didnt change a thing) I get this error:
Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment'
for this line of code:
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
and this error: Manifest merger failed : Attribute application#appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:12:5-42:19 to override.
in the event.
Here is the code:
package com.example.android.myfirstmap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney, Australia, and move the camera.
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
I looked around to see if there is a solution that fixes also my problem but none of what I tried was the right one.
does anyone knows how to fix it? evreything is updated.
Support Map Fragment extends androidx.fragment.app.Fragment.
You are importing android.support.v4.app.FragmentActivity which uses android.support.v4.app.Fragment. These are two different classes so they are incompatible.
You need to migrate your app to Android X: https://developer.android.com/jetpack/androidx/migrate
Then, in your build.gradle file, replace com.android.support:support-fragment with androidx.fragment:fragment.
Hope that helps!

Android Studio Google Map Incompatible Types Error

I am trying to set the Google Map in an Android Studio Project but facing incompatible types error even after applying different options couldn't get rid of it. Thanks for your time and guidance
Screenshot of error
Logcate report
Error:(454, 88) error: incompatible types: Fragment cannot be converted to SupportMapFragment
XML code
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map2"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Activity code
public class Search extends FragmentActivity implements OnMapReadyCallback
private void initializeMap()
{
SupportMapFragment map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map2)).getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
try to change this line
SupportMapFragment map = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map2)).getMapAsync(this);
to
SupportMapFragment map = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map2)).getMapAsync(this);
For more information, check this SO question

Material design fixed tabs without toolbar

I have a slight problem figuring out a "bug" in my code. Im not sure if its a bug or not. However, as you can see in the picture 1, the fixed tabs' position is behind the curtain drawer. I cant find/understand why this is. Ive followed several tutorials and they dont seem to have the same problem.
Ive tried to google it up, but i cant find a similar problem. Anyone experienced something similar before?
In the android studio, the design layout seems to be on point, however, not when compiled.
Im using the neokree lib so i can use the icons and ripple effect when selecting tabs. I've tried to use the google's tab layout link here, but as soon as i tried to remove the actionbar and apply the icons, the same problem occurred.
Thanks!
activity_main
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<it.neokree.materialtabs.MaterialTabHost
android:id="#+id/materialTabHost"
android:layout_width="match_parent"
android:layout_height="48dp"
app:iconColor="#color/iconColor"
app:primaryColor="#color/primaryColor"
app:accentColor="#color/accentColor"
app:hasIcons="true"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"/>
</LinearLayout>
styles.xml
<resources>
<style name="AppTheme" parent="AppTheme.Base"></style>
<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
<item name="colorControlHighlight">#color/colorHighlight</item>
</style>
</resources>
styles.xml v21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme. -->
<!--Using same style as in default style.xml file-->
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/primaryColorDark</item>
<item name="android:colorAccent">#color/accentColor</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:colorControlHighlight">#color/colorHighlight</item>
</style>
</resources>
MainActivity
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import it.neokree.materialtabs.MaterialTab;
import it.neokree.materialtabs.MaterialTabHost;
import it.neokree.materialtabs.MaterialTabListener;
public class MainActivity extends ActionBarActivity implements MaterialTabListener
{
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;
private MaterialTabHost tabHost;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
tabHost.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < adapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setIcon(adapter.getIcon(i))
.setTabListener(this));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onTabSelected(MaterialTab materialTab) {
viewPager.setCurrentItem(materialTab.getPosition());
}
#Override
public void onTabReselected(MaterialTab materialTab) {
}
#Override
public void onTabUnselected(MaterialTab materialTab) {
}
class ViewPagerAdapter extends FragmentStatePagerAdapter
{
int icons[] = {R.drawable.ic_home,
R.drawable.ic_graph,
R.drawable.ic_bell_mid,
R.drawable.ic_settings};
//String[] tabText = getResources().getStringArray(R.array.tabs);
public ViewPagerAdapter(FragmentManager fm)
{
super(fm);
//tabText = getResources().getStringArray(R.array.tabs);
}
#Override
public Fragment getItem(int position)
{
MyFragment myFragment = MyFragment.getInstance(position);
return myFragment;
}
//Attaching an image to a (spannable) string so we can show the image instead of text.
#Override
public CharSequence getPageTitle(int position){
/*Drawable drawable = getResources().getDrawable(icons[position]);
//icon bounds/size
drawable.setBounds(0,0,96,96);
ImageSpan imageSpan = new ImageSpan(drawable);
SpannableString spannableString = new SpannableString(" ");
spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;*/
return getResources().getStringArray(R.array.tabs)[position];
}
#Override
public int getCount()
{
return 4;
}
private Drawable getIcon(int position)
{
return getResources().getDrawable(icons[position]);
}
}
}
After accidentally getting over a youtube tutorial about making simple material design tabs, I made a new project and started to combine the one I found in youtube with my old one. Eventually, I found out that the following code line in the styles file was the cause of the problem.
<item name="android:windowTranslucentStatus">true</item>
It seems like it was something I added for testing and forgot to remove later on.
Try adding a Toolbar with a no actionbar theme..
In order to do this you have to make a new xml file called tool_bar.xml and paste the following code into it:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/ColorPrimary"
android:elevation="2dp"
android:theme="#style/Theme.AppCompat.NoActionBar"
xmlns:android="http://schemas.android.com/apk/res/android" />
You have then add this to your activity_main.xml file:
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>

Why is my app crashing in System.Windows.ni.dll on startup with no useful stack trace? (seemingly after adopting Caliburn.Micro)

Start with the default "empty" Windows Phone App project (Windows Phone 8). Let's call it BlackAdder for fun.
Add Caliburn.Micro from Nuget
Move MainPage.xaml to the Views subdirectory and create a matching ViewModel in ViewModels\MainPageViewModel.cs
Fix the namespace for MainPage to Blackadder.Views in both the .xaml and .xaml.cs files
Update App.xaml
<Application
x:Class="BlackAdder.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:local="clr-namespace:BlackAdder">
<Application.Resources>
<local:Bootstrapper x:Key="bootstrapper"/>
</Application.Resources>
</Application>
Update App.xaml.cs
public partial class App : Application
{
public static PhoneApplicationFrame RootFrame { get; private set; }
public App()
{
InitializeComponent();
// Show graphics profiling information while debugging.
if (Debugger.IsAttached)
{
...
}
}
}
Create Bootstrapper.cs
public class Bootstrapper : PhoneBootstrapper
{
private PhoneContainer container;
protected override void Configure()
{
container = new PhoneContainer();
container.RegisterPhoneServices(RootFrame);
container.PerRequest<MainPageViewModel>();
AddCustomConventions();
}
protected static void AddCustomConventions()
{
}
protected override object GetInstance(Type service, string key)
{
return container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
container.BuildUp(instance);
}
}
When we run the app, it crashes in a very weird place.
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in System.Windows.ni.dll
Additional information: Exception has been thrown by the target of an invocation.
There is no inner exception, and the stack trace is (seemingly) useless.
System.Windows.ni.dll!System.Windows.Threading.DispatcherOperation.Invoke()
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority)
System.Windows.ni.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context)
System.Windows.ni.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args)
System.Windows.RuntimeHost.ni.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam* pParams, System.Windows.Hosting.NativeMethods.ScriptParam* pResult)
It turns out this has nothing to do with caliburn.micro at all. The problem lies in Properties\WMAppManifest.xaml.
After moving MainPage.xaml to the Views subdirectory, the Navigation Page setting (found on the first tab, Application UI) needs to be updated to Views\MainPage.xaml

JavaFx | Search and Highlight text | Add Search Bar for loaded web page

I used the SimpleSwingBrowser example (http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm) and added some code of my own for log tailing.
I wanted to add a search bar ability to it (Search and Highlight text).
After googling for hours and self experiments, I didn't find a way to do it.
Can someone give me a kick-off direction for writing such ability.
Suggestions for a JavaScript based solution
Use an existing JavaScript highlighting library such as jQuery highlight or hilitor.js.
Suggestions for a Java based solution
Use the Java w3c DOM API to perform operations on the WebEngine document object after the document has been loaded.
To get a Search API in the JavaFX WebView core implementation
I created feature request RT-23383 Text search support for WebView. The feature request is currently open and unactioned - you can create an account in the issue tracker and vote for or comment on the feature request.
Sample
This sample uses jQuery highlight. The user types the word to be highlighted into the text field, then presses the highlight button to highlight all occurrences of the word in the page or to remove highlight button to clear all marked highlights. You could modify the sample to allow further jQuery based searches to scroll to a next and previously highlighted word.
I tried to get it to work with any arbitrary web page, but that logic defeated me. If you control the source of the page you want to search and can add the reference to the jQuery highlight plugin and it's style class to your page, something like this sample program might be an option.
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.*;
import javafx.stage.Stage;
public class WebViewSearch extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final WebView webView = new WebView();
final WebEngine engine = webView.getEngine();
engine.load("http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html");
final TextField searchField = new TextField("light");
searchField.setPromptText("Enter the text you would like to highlight and press ENTER to highlight");
searchField.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
if (engine.getDocument() != null) {
highlight(
engine,
searchField.getText()
);
}
}
});
final Button highlightButton = new Button("Highlight");
highlightButton.setDefaultButton(true);
highlightButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
searchField.fireEvent(new ActionEvent());
}
});
final Button removeHighlightButton = new Button("Remove Highlight");
removeHighlightButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
removeHighlight(
engine
);
}
});
removeHighlightButton.setCancelButton(true);
HBox controls = new HBox(10);
controls.getChildren().setAll(
highlightButton,
removeHighlightButton
);
VBox layout = new VBox(10);
layout.getChildren().setAll(searchField, controls, webView);
searchField.setMinHeight(Control.USE_PREF_SIZE);
controls.setMinHeight(Control.USE_PREF_SIZE);
controls.disableProperty().bind(webView.getEngine().getLoadWorker().runningProperty());
searchField.disableProperty().bind(webView.getEngine().getLoadWorker().runningProperty());
primaryStage.setScene(new Scene(layout));
primaryStage.show();
webView.requestFocus();
}
private void highlight(WebEngine engine, String text) {
engine.executeScript("$('body').removeHighlight().highlight('" + text + "')");
}
private void removeHighlight(WebEngine engine) {
engine.executeScript("$('body').removeHighlight()");
}
}