Jetpack databinding generated duplicate class in same file as same class - java

This morning I got an error where my app won't install and I have traced it down to this problem. I have two of the same class in the same file. I don't want to include the entire generated file as it is very long.
public class AddVinDialogFragmentBinding extends androidx.databinding.ViewDataBinding implements android.databinding.generated.callback.OnClickListener.Listener {
#Nullable
private static final androidx.databinding.ViewDataBinding.IncludedLayouts sIncludes;
#Nullable
private static final android.util.SparseIntArray sViewsWithIds;
:
#Nullable
private com.ui.EventHandler mHandler;
#Nullable
private final android.view.View.OnClickListener mCallback1;
public class AddVinDialogFragmentBinding extends androidx.databinding.ViewDataBinding implements android.databinding.generated.callback.OnClickListener.Listener {
#Nullable
private static final androidx.databinding.ViewDataBinding.IncludedLayouts sIncludes;
:
}
In gradle.properties I have this: android.databinding.enableV2=true
My layout is pretty simple:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="vin"
type="String" />
<variable name="handler" type="com.ui.garage.handlers.EventHandler" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ui.AddVinDialogFragment">
<TextView
android:layout_width="wrap_content"
android:text="Please enter a VIN"
android:layout_height="wrap_content" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/add_vin_field"
android:layout_width="200dp"
android:maxLength="17"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/vin"
android:text="#={vin}" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/button"
android:onClick="#{() -> handler.addVin(vin)}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
</layout>
I am using Android Studio 3.3 Canary 3
Sometimes I get that there is a duplicate class in my layout file and then it can't find the DataBindingComponent. What is annoying is that this worked yesterday and I haven't changed the binding since then.
Any suggestions as to what may be the issue?
I have deleted .gradle and mobile/build/* and did a clean and rebuild multiple times, as well as exiting and restarting Android Studio.
I have three layout files with databinding and all three have the same errors.

Delete the compiler dependency, it is provided with binding plugin from 3.2.0. At least it worked for me.
kapt "com.android.databinding:compiler:$gradleVersion

Related

How to integrate Stripe Payment in Android Java?

I need a Stripe Implementation for my Java Android App, but with a Server in Java, I don't know how to use node or php to implement that server-side and I found something on their docs:
https://stripe.com/docs/payments/accept-a-payment?integration=elements#android-create-payment-intent
but, the server-side is poorly explained.
All I've wrote until now is :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:padding="30dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Checkout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/logo"
android:layout_gravity="center"
/>
<TextView
android:id="#+id/youhavetopay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textColor="#color/colorPrimary"
android:fontFamily="#font/muli_black"
android:layout_gravity="center"
/>
<com.stripe.android.view.CardInputWidget
android:id="#+id/cardInputWidget"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/payButton"
android:layout_gravity="center"
android:layout_marginTop="20dp"
/>
</LinearLayout>
</ScrollView>
and this
public class Checkout extends AppCompatActivity {
Button payBtn;
String message;
String price;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
payBtn = findViewById(R.id.payButton);
text = findViewById(R.id.youhavetopay);
Bundle extras = getIntent().getExtras();
if(extras != null) {
message = extras.getString("message");
price = extras.getString("passprice");
}
String t = "Pay $"+price;
payBtn.setText(t);
text.setText(message);
}
}
I have these implementations :
implementation "com.stripe:stripe-java:15.1.0"
implementation 'com.stripe:stripe-android:15.1.0'
implementation 'com.squareup.okhttp3:okhttp:4.4.0'
implementation 'com.google.code.gson:gson:2.8.6'
I really don't understand what I have to do from their docs. I only want a pay button that makes a payment from that card and if its accepted, do something on the database. A simple payment.
If you can guide me please, I would appreciate it! Any tutorials or advices are welcomed. I've tried to copy from the GitHub, but I saw many red lines from imports and I got lost.
Thank you in advance!
This sample includes both example Android client and a Java server implementations. You should be able to clone the repository and follow the appropriate READMEs to run those examples and see how this works. Follow along with the guide and if you have more specific questions please raise those.
You will need to be able to install and build android & java to run this, of course.

Android cannot find BindingConversion in include tag

I have made a BindingConversion from boolean to visibility, however Android can't find it, but only when I use it in an include tag. It works at other elements like FrameLyout.
In my abstract ViewModel:
#BindingConversion
public static int convertBooleanToVisibility(boolean visible) {
return visible ? View.VISIBLE : View.GONE;
}
Then in my xml (TestViewModel inherits from ViewModel):
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="oliveradam.testapp.viewmodels.TestViewModel"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
app:theme="#style/toolbarStyleMusicView"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
<include
android:visibility="#{viewModel.isVisible}"
layout="#layout/layout_test"
app:viewModel="#{viewModel}" />
</LinearLayout>
</layout>
Error:(139, 29) error: cannot find symbol method setVisibility(boolean)
I'm working on Android Studio 2.2 RC.
I don't know why this is not working, but I think its the layout tag in include. You can't use these tags for databinding. So I deleted the includes, used a viewstub and changed the layouts programmatically.

"R cannot be resolved to a variable " in android Activity

i was doing small android application , i added blank xml file but it was showing some error although i did not add any code and still it was showing error so i cleaned project but now after cleaning for my activity when i write
setContentView(R.layout.reminder_edit);
its giving errors like this R cannot be resolved to a variable ,
for another activity named ReminderListaActivity i tried this
setContentView(R.layout.reminder_edit)
error was for xml file not for "R " error was :reminder_edit cannot be resolved or is not a field .
xml file is in layout and its showing like never used file so how do i solve this issue , can someone help me with it pleae
EDIT code added
reminder_edit.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:text="#string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/body"
/>
<EditText
android:id="#+id/body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="5"
android:scrollbars="vertical"
android:gravity="top" />
<TextView
android:text="#string/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/reminder_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Reminder_Time"
/>
<TextView
android:text="#string/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/remindertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/remindertime"
/>
<Button
android:id="#+id/confirm"
android:text="#string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
java File
public class ReminderEditActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reminder_edit);
}
}
Fix the errors in XML so the resource compiler can generate R.java for you.
Remove Import android.R; from imports in your activity class
In order to accesst "R.layout.edit_reminder" xml file you must import your "R" file which is found under the "gen" folder. This file is automatically generated, so you can go ahead and delete it and it will be generated again. Now, if you have any xml errores whatsoever the R file won't be generated, that's a sign that you have to look to your xml to find the problem.
In regards to the import, you'll find them on your Activity Class, at the very top right after the package declaration.
Make sure that the R you are importing is:
yourpackage.R;
rather tahn
android.R;
When writing code quickly at times android.R gets imported and you don't even notice. That's the first thing I always check, it's happened to me a couple of times already.
Next thing I would suggest doing is looking to see your console and problems view (if these are not set in your perspective, then go to Window>Show View>Other and select the appropriate ones).
Look for something along the lines of Error with xml file aborting build. Look for what xml file it is talking about, and then look for errors in it.

Why does my findViewById() not find my Id that I have introduced in my xml code?

I am very new to Android app development. But in one of my first projects I have come across an error. My findViewById() does not find an Id that has been introduced in the xml code.
Here is my Java code:
package com.example.fuzzylogic;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSubtract);
}
#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;
}
}
Here is my xml 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: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=".MainActivity" >
<TextView
android:id="#+id/tvDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Your total is 0"
android:textSize="45dp" />
<Button
android:id="#+id/bAdd"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_below="#+id/tvDisplay"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="add one"
android:textSize="25dp" />
<Button
android:id="#+id/bSubtract"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/bAdd"
android:layout_below="#+id/bAdd"
android:layout_marginTop="23dp"
android:text="substract one"
android:textSize="25dp" />
</RelativeLayout>
My findViewById() does not find an Id that has been introduced in the
xml code.
So this may cause:
Errors in XML file(s) and then R.java is not correctly generated
You did not save all edited xml files(ctrl+s)
So make it sure that you don't have errors in your project and clean and rebuild your project.
First you must save the xml file and the rebuild it
When u add a new file or changes to resource file , you better rebuild the project ,it will modify your R.java file corresponding to the changes you have made
and in findViewById() the id that we are passing is not the id from your layout xml file . actually it's an integer representation of the resource objects in R.java file
Note : If you are using eclipse chose automatically build option selected
I had a same issue, the reason why i was getting the issue was that R class was not updated with the new Xml file resources that i had added. So i rebuild the project and boom the issue was resolved.
So rebuilding the project might help.

findViewById Returns An Error

findViewById gives out an error, I think it's because it's returning null. My app is forced to close.
SensorManager sensorManager=null;
TextView x= null;
TextView y= null;
TextView z= null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.main);
x= (TextView) findViewById(R.id.tv1);
y= (TextView) findViewById(R.id.tv2);
z= (TextView) findViewById(R.id.tv3);
}
XML:
<TextView
android:id="#+id/tv1"
android:layout_x="10px"
android:layout_y="550px"
android:text="#string/x"
/>
<TextView
android:id="#+id/tv2"
android:layout_x="100px"
android:layout_y="550px"
android:text="#string/y"
/>
<TextView
android:id="#+id/tv3"
android:layout_x="200px"
android:layout_y="550px"
android:text="#string/z"
/>
Can someone please tell me what I'm doing wrong?
SOLVED!
I used the XML given by Sunny at the bottom! Thank you to all!
That's simple.... tv* is not in your layout!
Please post the layout, I am 100% sure it doesn't contain:
<TextView android:id="#+id/tv1"/>
<TextView android:id="#+id/tv2"/>
<TextView android:id="#+id/tv3"/>
Please also learn how to get a logcat before going further in android development! ;-)
Window -> Show View -> Logcat
yes above code working fine and xml file should be like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView>
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
yes that problem and if with xml is not done then
You import android.R;
so that it will give error if xml you have
remove that
and import PACKAGENAME.R;
First : Verify that your TextView Are present on your layout : main.xml :
<TextView android:id="#+id/tv1" .../>
<TextView android:id="#+id/tv2" .../>
<TextView android:id="#+id/tv3" .../>
Second : reBuild your project by following this steps : Project ==> Clean ==> Select your project and click OK .
The reason why this happens is :
findViewById() will search for the TextViews in the View hierarchy established by setContentView(R.layout.main). So if the TextViews aren't in main.xml you will get a null

Categories