ClassCastException casting chronometer.java file - android

I'm using Kotlin. I tried using the Millisecond Chronometer found here https://github.com/antoniom/Millisecond-Chronometer. I downloaded the zipfile, and then copied the Chronometer.java file into a new .java file in my project.
I placed a chronometer widget in the layout file and gave its id chrono
When I tried to use Chronometer.java, it throws me an error:
val mChronometer = findViewById<Chronometer>(R.id.chrono)
//Caused by: java.lang.ClassCastException: android.widget.Chronometer cannot be cast to com.example.rinor.chronometertimer.Chronometer
How to fix this? I'm sure if I get this line right everything else is smooth sailing from there on.

Probably you use the wrong Chronometer in your .xml <Chronometer>...</Chronometer> instead of
<com.example.rinor.chronometertimer.Chronometer
android:layout_width="match_parent"
android:layout_height="wrap_content">...
</com.example.rinor.chronometertimer.Chronometer>

Probably you import "android.widget.Chronometer" instead of "com.example.rinor.chronometertimer.Chronometer" change the import.

Related

Android Studio Template package name

I tried make android studio template like activity template by this instruction
Current package set in globals.xml.ftl
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}" />
and create template file
<instantiate from="src/app_package/LifecycleFragment.java.ftl"
to="${escapeXmlAttribute(srcOut)}/${className}.java" />
It force my template file to src/main/java/myPackageName
But my current package is kotlin
How can i create template in current selected folder?
Sorry if I'm late with my answer, but maybe it will be useful for someone.
To change the source package from java to kotlin try add this line to globals.xml.ftl:
<global id="kotlinMainSourceSet" value="${srcOut?replace('java','kotlin') />
Use it in your receipt.xml.ftl as following:
! Replace SomeClass.kt.ftl with your class in app_package;
! Replace your_needed_path with path, in which you would like to put the file
! Replace someClassName with the id of the className from template.
<instantiate
from="src/app_package/SomeClass.kt.ftl"
to="${escapeXmlAttribute(kotlinMainSourceSet)}/${your_needed_path}/${someClassName}.kt" />
In addition I'll answer on the #AlexeiKorshun question with which I've faced too: applicationIdSuffix is added, while creating a directory. This can be easily fixed by overriding srcOut in your globals.kt.ftl with following:
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName?replace('.dev|.qa2|.stage', '', 'r'))}" />
.dev/.qa2/.stage are added as an example,please replace them with yours.

Xamarin:Unable to convert instance of type Android.Widget.LinearLayout to type Android.Support.V7.Widget.Toolbar

The app is running on Android 6 or below. But when running on Android 7 (Nougat) it throws runtime exception.
System.InvalidCastException: Unable to convert instance of type 'Android.Widget.LinearLayout' to type 'Android.Support.V7.Widget.Toolbar'.
The error is thrown in Xamarin (Visual studio 2017) at below code:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle); //Runtime error here
.... Other code ...
}
}
I've taken latest updates for All Nuget packages but not able to identify the issue. For testing I'm using Moto G4+ (Android 7.0).
Can anybody help please?
Edit 1:
Layout of toolbar is as : (Under resources > layout > toolbar.axml). This was already there when App version with Android 6 released. But Now visited app back for Android 7 issues.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
I solved this by just cleaning the entire solution and closing the emulator. Then deploy again and it should work.
I tried SeƱor Hernandez's answer without much success, then realized I had accidentally deleted Resources/Resource.designer.cs from my Android project. I had meant to delete it from disk only, expecting it to get regenerated on rebuild. It did get regenerated, but that did not add it back to the project. Doing so (right-click, include in project) solved the issue on my part.
It got solved by downloading android_m2repository_r29.zip repository from https://developer.xamarin.com/guides/android/troubleshooting/resolving-library-installation-errors/ with instruction given in same.
I also tested the issue by creating a new fresh solution and got some style "Resource not found error". That was also fixed and then the original problem also got fixed.
You get this kind of error when trying to assign a control (a UI element in your xml) on a variable that is not of the same type. I get this error frequently when I make a mess and try to assign an EditText to a TextView variable or vice versa, i. e
EditText myText = FindViewById<TextView>(Resource.Id.myText);
or
TextView myText = FindViewById<EditText>(Resource.Id.myText);
I'm pretty sure that in your case you are trying to assign an Android.Widget.LinearLayout to a variable of type Android.Support.V7.Widget.Toolbar.
I think in your case one of this variables have been defined as LinearLayout in your xml:
Resource.Layout.toolbar
Resource.Layout.tabs
Meanwhile, one of this variables have beeen defined as Toolbar in your C# code, so te cast is not possible:
ToolbarResource
TabLayoutResource
If the problem persist, you can also try to check the type Android.Support.V7.Widget.Toolbar, instead it could be Android.Widget.Toolbar in your xml or code.

MvxSpinner/MvxListView doesn't work with MvvmCross Light (Chimp) from axml

In Android project I'm trying to add databindings using CrossLight part of MvvmCross.
Bindings to standard TextView/Buttons work great. But simplest markup with Mvx.Control:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<Mvx.MvxListView />
</LinearLayout>
Gives an error
"Binary XML file line #1: Error inflating class Mvx.MvxListView"
The same thing is with Mvx.Spinner.
However, when instantiating it from code in Activity.OnCreate:
_bindingContext = new MvxAndroidBindingContext(this, new LayoutInflaterProvider(LayoutInflater), _viewModel);
var view = (LinearLayout)_bindingContext.BindingInflate(Resource.Layout.Main, null);
SetContentView(view);
var spinner = new MvxSpinner(this, null, new MvxAdapter(this, _bindingContext));
view.AddView(spinner);
Everything works great (including bindings). What am I doing wrong? Is this scenario supported in general?
Or maybe I should reference anything else except nuget MvvmCross.HotTuna.CrossCore?
P.S. Haven't found any samples with custom controls and CrossLight neither on github, nor on N+1 videos
If you want to use namespace abbreviations within your non-MvvmCross application, then you'll need to add those abbreviations. This can be done using a custom binding builder or using a 'light' setup step like:
var viewResolver = Mvx.Resolve<IMvxAxmlNameViewTypeResolver>();
viewResolver.ViewNamespaceAbbreviations["Mvx"] = "Cirrious.MvvmCross.Binding.Droid.Views";
viewResolver.ViewNamespaceAbbreviations["MyApp"] = "MyApp.Controls";
When doing this within a full MvvmCross application, then you can override the Setup property ViewNamespaceAbbreviations
protected override IDictionary<string, string> ViewNamespaceAbbreviations
{
get
{
var toReturn = base.ViewNamespaceAbbreviations;
toReturn["MyApp"] = "MyApp.UI.Droid.Controls";
return toReturn;
}
}
When markup was changed to using the full namespace and layout_width and layout_height attribute was added it started to work!
<Cirrious.MvvmCross.Binding.Droid.Views.MvxSpinner
android:id="#+id/MySpinner"
android:layout_width="fill_parent"
android:layout_height="20dp"
/>
It was found when I switched to default Android inflater and it was complaining about missing layout_width in Exceptions.

Admob 6.1.0 for monodroid

I have an app made with monodroid, it's on release stage (ready to get released) but I would like to have a free ad-supported version of my app. I've been searching the web for a tutorial of Admob 6.1.0 implementation on monodroid without any luck. I'd have to mention that I've never worked with ads before.
I was wondering if anyone has been able to use Admob 6.1.0 on monodroid and if you could share your knowledge
I've seen this source code and also this tutorial, but I just can't manage to understand correctly how to implement it. I't would be nice if someone could make an answer as a community wiki so it can help others to get introduced to Admob in monodroid
EDIT:
Tried Greg Shackles sample step by step, i'm now getting this error android.view.InflateException: Binary XML file line #1: Error inflating class com.google.ads.AdView. Any sugestion of how to make it work?
EDIT 2:
Changed the XML file and now getting 2 new errors: Java.Lang.NoClassDefFoundError and System.TypeInitializationException. XML looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/Background2"
>
<com.google.ads.AdView android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="AD_UNIT_ID_GOES_HERE"
ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
ads:loadAdOnCreate="true"/>
</RelativeLayout>
And adview is called like this:
public class MyActivity : Activity
{
private View _adView;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView(Resource.Layout.AboutActivityLayout);
AdMobHelper.RegisterEmulatorAsTestDevice();
_adView = FindViewById(Resource.Id.Ad);
AdMobHelper.RequestFreshAd(_adView);
loadData();
}
}
The first error you got "android.view.InflateException: Binary XML file line #1: Error inflating class com.google.ads.AdView" is related to the Jar Build Action setting.
You should check two things first:
the jar file has the Build Action property set to "AndroidJavaLibrary"
the java file called "AdMobHelper.java" should have the Build Action property set to "AndroidJavaSource"
It seems to me that the error you are getting now is related to the java file not being correctly configured as an AndroidJavaSource.

EditText cannot be resolved to a type

I have defined a layout in an xml file in the 'res' folder of my android project. The 'EditText' element looks like:
<EditText android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numeric="integer|decimal"></EditText>
In my class file in my android project, I have the following:
public void doCalculation(View view) {
String firstNo = ((EditText) findViewById(R.id.editText1)).getText().toString();
String secondNo = ((EditText) findViewById(R.id.editText2)).getText().toString();
String operator = ((Spinner) findViewById(R.id.spinner1)).getSelectedItem().toString();
callWebService(firstNo, secondNo, operator );
}
Unfortunately, the first 2 assignments in my method above are showing an error in eclipse stating
EditText cannot be resolved to a type
I've no idea how to fix this. I'm using android 2.3.3 API 10.
Any help would be appreciated. Thanks
You need to import the EditText class, so it's known, using the following line at the beginning of your .java file :
import android.widget.EditText;
Note that, in most cases, Eclipse can help you a lot : it has an Organize Imports feature, that will add the required import lines :
Menu > Source > Organize Imports
Or use Ctrl + Shift + O
Did you try adding this manually?
import android.widget.EditText;
Also check your console & error log for additional errors. Usually with things this obvious the reason can be something else too.
If the import doesnt work, try closing and reopening your project.
If you tried to ad import android.widget.EditText, and doesn't worked try to clean your project at Project -> Clean... and Try to click with right mouse button on your project choose Android tools then fix project properties. Hope it helps.
If none of the other answers work, you can always do this:
EditText txt1 = (EditText)findViewById(R.id.editText1);
EditText txt2 = (EditText)findViewById(R.id.editText2);
String firstNo = txt1.getText().toString();
String secondNo = txt2.getText().toString();
Check for imports.
To get the text from EditText try get the TextView value. It might work.
String firstNo = ((TextView) findViewById(R.id.editText1)).getText().toString();
String secondNo = ((TextView) findViewById(R.id.editText2)).getText().toString();
Above your class, just import:
import android.widget.EditText;

Categories

Resources