I have a library with an LegendActivity using layout legend.xml which references a view with id "icons". Now, in my app, I subclass this activity with "AppLegendActivity" and use the app's overriding legend.xml which does not include any view with id "icons".
Library
public class LegendActivity extends Activity {
#Override
public void onCreate(Bundle b) {
setContentView(R.layout.legend);
View icons_view = findViewById(R.id.icons);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/icons"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
App
public class AppLegendActivity extends LegendActivity {
#Override
public void onCreate(Bundle b) {
super.onCreate(Bundle b);
View app_icons_view = findViewById(R.id.app_icons);
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/app_icons"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
This is an over-simplification of my actual code, but this is the crux of the problem. When I run pro-guard I get the following error:
Warning: com.example.activity.LegendActivity: can't find referenced field 'int icons' in program class com.example.R$id
If I add the "-dontwarn" flag in proguard, I can get the app to compile, but as soon as the AppLegendActivity calls super.onCreate, the reference to R.id.icons gets called and the app crashes with:
ava.lang.NoSuchFieldError: com.example.r.icons
App's build.grade referencing proguard section:
buildTypes {
release {
// Proguard
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
My proguard-rules.pro
-dontwarn com.example.**
Can this code be re-factored to avoid this crash? Yes. Can I simply rename a few resources files and get this conflict to go away? Sure. But in the actual project, this much more complicated and I don't know the full extend of the implications of such a refactor. Is there a way to get the library's R file to not remove/obfuscate IDs that are no longer visible after the app's overriding of resources?
In AppLegendActivity class you override LegendActivity you need change setContentView(R.layout.....); or remove super.onCreate(Bundle b);
Related
I am getting an error of 'Resource.Layout' does not contain a definition for 'activity_main'. But I am confused, why it is showing this type of error. Because activity_main is available in Resource.Layout, but still showing the same error.
Here is the code,
Resources => Layout => activity_main.axml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView" />
</LinearLayout>
Resources => MainActivity.cs
[Activity(Label = "BSSTMatrimonial", MainLauncher = true)]
public class MainActivity : Activity
{
WebView webView = null;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
// webView = FindViewById<WebView>(Resource.Id.webView);
//webView.SetWebViewClient(new ExtendWebViewClient());
webView.LoadUrl("www.bsstmatrimony.com");
WebSettings webSettings = webView.Settings;
webSettings.JavaScriptEnabled = true;
}
}
Try to Clean & Rebuild your solution. Even you can delete & re-add .axml file.
If still won't work write click on .axml file
Go to properties -->Build action
Make sure Build action set to AndroidResource.
It is a harmless error that can be ignored but you can search your files for Resource.Designer.cs. It is likely that you have two, one in your project's obj/debug folder and one in your project's Resources folder. Check for activity_main in both, it is possible that the file in your Resources folder doesn't have it.
I've been encountering this problem a lot. I think the problem lies in the fact that as xamarin is updated, it changes the variable names and file names in the templates. So if you are using code from a tutorial, if you are seeing this problem, its likely outdated. This means that you have to rename something to fit the current template.
In this case I had to change
SetContentView(Resource.Layout.Main);
to
SetContentView(Resource.Layout.activity_main);
After upgrading to Gradle 2.3. My project cannot compile. I'm having the log in the console
incompatible types: ObservableInt cannot be converted to int
Look at the generated file
android.databinding.ObservableInt viewModelLoadingVisibility;
this.vLoading.getRoot().setVisibility(viewModelLoadingVisibility);
In xml file
<android.support.v7.widget.RecyclerView
android:id="#+id/rvProducts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="#{viewModel.contentVisibility}"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
I tried in method in my binding class
#BindingAdapter("app:visibility")
public static void setViewVisible(View view, ObservableInt visible) {
int visibility = visible.get();
view.setVisibility(visibility);
}
and got log
warning: Application namespace for attribute app:visibility will be ignored.
public static void setViewVisible(View view, ObservableInt visible) {
warning: Use of ObservableField and primitive cousins directly as method parameters is deprecated and support will be removed soon. Use the contents as parameters instead in method public static void setViewVisible(android.view.View,android.databinding.ObservableInt)
public static void setViewVisible(View view, ObservableInt visible) {
Anyone encounters this?
This looks like a bug. Please file it. There are many tests and we don't expect this kind of regression. It is important that we get your specific example so we can be sure it is caught.
You can ignore the warnings for now.
The first is caused because of this:
#BindingAdapter("app:visibility")
You should use this instead:
#BindingAdapter("visibility")
The second is because we support ObservableInt as a parameter. You typically don't want to accept ObservableInt, but rather int instead. I'd love to see use cases where ObservableInt is necessary. We may just remove that warning and support it always or we may pull the plug on supporting ObservableInt as a parameter if there are no valid uses.
----- edit -----
I tested this with a little application and I didn't have any issue without any BindingAdapter. Here is the layout:
<layout>
<data>
<variable name="model" type="com.example.gmount.testobservableint.MyModel"/>
</data>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="#{model::clicked}"
tools:context="com.example.gmount.testobservableint.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="#{model.visibility}"
/>
</android.support.constraint.ConstraintLayout>
</layout>
Here is my model:
public class MyModel {
public final ObservableInt visibility = new ObservableInt(View.VISIBLE);
public void clicked(View view) {
int oldVisibility = visibility.get();
int newVisibility = oldVisibility == View.VISIBLE ? View.GONE : View.VISIBLE;
visibility.set(newVisibility);
}
}
Even when I used a BindingAdapter taking an ObservableInt, it worked. Here's my BindingAdapter:
#BindingAdapter("visiblity")
public static void setVisibility(View view, ObservableInt visibility) {
view.setVisibility(visibility.get());
}
And I changed the View's binding to be:
<TextView ...
app:visibility="#{model.visibility}"
/>
Is there something different about your viewModel?
You just have to add this to the bottom of your build.gradle dependencies:
apt 'com.android.databinding:compiler:2.3.0'
See this: https://stackoverflow.com/a/42711830/376829 regarding GoMobile update to version "+eb90329 Mar 7 2017" and GoBind plugin revert to version "0.2.6" (although the current version is "0.2.8")
android:visibility="#{viewModel.contentVisibility}"
remember this
dataBinding {
enabled = true
}
re-download the library from the Support repository in the Android SDK manager.
I'm new to Xamarin Android. I've installed the AppCompatv7 Support Library from the GetComponents option.
But whenever I try and do anything with it, I get no intellisense, like it isn't actually added to the project. Like below:
When I look more into the assembly it comes up with an option like below, saying it might not be installed. But as you can see from the picture, it is installed under my references.
If I click Add Package in the below picture, nothing happens.
When I compile the code, it can't find functions in the ActionBarActivity base class, so I'm guessing it's not adding it properly into my project.
Anyone know why this is happening? Cheers
When I compile the code, it can't find functions in the ActionBarActivity base class, so I'm guessing it's not adding it properly into my project.
ActionBarActivity is obsolete.
To use Android.Support.V7.Widget.Toolbar, after installing the Xamarin.Android.Support.v7.AppCompat package, you can simply inherit your MainActivity from AppCompatActivity instead of ActionBarActivity.
Then for example my toolbar is like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toolbartitile"
android:text="Kodej" />
</android.support.v7.widget.Toolbar>
Include this toolbar in Main layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/toolbar"
layout="#layout/mytoolbar" />
</LinearLayout>
And finally in your MainActivity:
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
SetSupportActionBar(toolbar);
}
}
You just need to reference Android.Support.V7.App in your code:
using Android.Support.V7.App;
All references in my demo:
If you've installed the libs correctly and there is no no intellisense, you can try to rebuild your app, close and reopen your VS.
Install it using the NuGet Command Console :
Install-Package Xamarin.Android.Support.v7.AppCompat -Pre
I'm trying to use the fresco library. I used it before too and it was working, but now, for some reason I get:
Unable to start activity ComponentInfo{com.example.home.template/com.example.home.template.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class com.facebook.drawee.view.SimpleDraweeView
My xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.drawee.view.SimpleDraweeView
android:id="#+id/profileImage"
fresco:actualImageScaleType="centerCrop"
android:layout_width="200dp"
android:layout_gravity="center_horizontal"
android:layout_height="200dp" />
</LinearLayout>
MyApplication:
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
FacebookSdk.sdkInitialize(this);
}
}
I have it in my manifest: android:name=".MyApplication"
The only problem that I'm having is with the draweeview. I can do all of the other stuff such as logging in and taking information.
In my case writing Fresco.initialize(this); before setContentView(R.layout.myxml); helped me.
Update:
you have FacebookSdk.sdkInitialize(this); instead of Fresco.initialize(this) in your myapplication
I was getting this problem in API 19 devices just because I was using drawable vector as placeholder here
fresco:placeholderImage="#drawable/ic_phone"
After changing to PNG my problem was solved.
Dont forget to initialize it in your App as
class App : Application() {
override fun onCreate() {
super.onCreate()
Fresco.initialize(this)
}
}
as well as this line in manifest's application
android:name=".App"
Today I had the same problem. However, I forgot to add the property android:name=".MyApplication" in the AndroidManifest.xml.
In your Application class write the following line:
Fresco.initialize(this);
Make sure that you import the right Fresco library and if you do import other Fresco libraries, make sure the other libraries version number is the same with Fresco core library
I did everything just as stated in this tutorial:
google android basic tutorial
and despite everything being done just as described, the code refuses to compile with 3 errors. Looks like the guys writing the turorial forgot to mention what are those things and where/how do I define them.
The errors I get:
Error:(24, 68) error: cannot find symbol variable container
Error:(36, 23) error: cannot find symbol variable action_settings
Error:(46, 54) error: cannot find symbol variable fragment_display_message
Neither of the 3 fields are defined anywhere (Perhaps one of the libraries is wrong?)
The file in question is:
package com.example.asteroth.first;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.TextView;
import android.R;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
// setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message, container, false);
return rootView;
}
}
}
I am using Android Studio I just downloaded and no question from search or Similar Questions points at the problem like this one, hence I suspect authors of tutorial forgot to mention something minor. I've seen suggestion to place the "container" as a new ID in one of the XML files, but to no avail.
EDIT:
'cannot find symbol ActionBarActivity' following Android Development Tutorial?
This post suggest a solution, however it changes ActionBarActivity to just Activity which is very different from what the tutorial uses and I don't know how serious repercussions would it cause
EDIT2:
Problems found and removed:
import android.R //causes action_settings error
container missing //had to add it in the xml file as an id
xml file named wrong //If I got that correctly, I'm still waiting for someone experienced to clarify, but seems like the tutorial used different name for the xml file then the one that the java code references
Remaining problem is similar to this one
Cannot resolve method placeholderfragment error
however, I both extend Fragment and include android.app.Fragment as can be seen in the included file.
I tried the same tutorial and here is how I fixed my errors:
R.id.container cannot be resolved error
I had to import android.support.v4.app.Fragment to fix this problem and add android:id = "#+id/container" to the RelativeLayout section in my activity_display_message.xml file.
fragment_display_message cannot be resolved error
Change R.layout.fragment_display_message to R.layout.activity_display_message instead. There is no need for creating a new xml file for fragment_display_message.
This should fix these two errors.
But you would probably be better off if you comment out the if(savedInstanceState............ statement as otherwise your program would crash once you try to run it if it doesn't give you any errors.
Your onCreate method should look like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView=new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
/*if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}*/
}
I'm doing the tutorial for the first time now on Feb. 23, 2015 and ran into this compilation error though I feel like I've closely followed the steps. I changed fragment_display_message to activity_display_message which is an XML file they have us create in the tutorial. This seems to solve the error, and allow the app to run.
// A placeholder fragment containing a simple view.
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
Add this line to take care of your first error: android:id = "#+id/container"
You get that error because container isn't in the XML.
Add <string name="action_settings">Action Settings</string>so that the "Action settings" which I'm assuming doesn't exist in your XML code since you have that error.
Create your own XML file with this exact name fragment_display_message.xmlto handle that error and check what code you might need to insert into it in your google tutorial. Often times with Eclipse, these files are not included for reasons outside my knowledge. So you have to create them or insert them yourself. (Make sure you have the latest version of the SDK by the way.
EDIT: Be sure to have the correct imports matching with your "tutorial". I took a gander at it and see you missing two imports. One of which another user answered.
It's a copy paste error.
If you paste code with "R." in it, the development environment always imports the android.R:
import android.R;
If you use R.id.... it is always looking up the android.R and not your own generated R class.
Delete the import and it should be fine. This general works for me.
After that you have to check if you already defined the id's and layout.
You can check Layouts by looking on the package explorer under res->layout. In your example there has to be an fragment_display_message.xml in it.
For id's you have to look up all of your layouts and check if there are the specific views like the container.
I got a similar error on the Building a Simple User Interface step:
Error:(18, 54) error: cannot find symbol variable toolbar
I've narrowed the cause down to res/layout/activity_my.xml:
<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="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
The original version that did compile (but no button or text box) is:
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MyActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_my" />
<android.support.design.widget.FloatingActionButton android:id="#+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
fragment_display_message
Make sure you have a file named fragment_display_message.xml in your res/layout folder.
action_settings
Make sure you have that item in your menu.xml file in res/menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=
".MenuExampleActivity" >
<item
android:id="#+id/action_settings"
android:orderInCategory="1"
app:showAsAction="never"
android:title="My menu option"/>
</menu>
container
Make sure you have a layout (ex. RelativeLayout) with the id set to "container" in your activity_main.xml file in res/layout, given that it's the reference for the code to insert the fragment there.