findViewById Returns An Error - java

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

Related

How to show a snakbar with Lottie animation in start from a recycler view adapter

I am making a chat app and I want to add a feature like telegram to copy links ,phone numbers and etc.. from long clicking on an auto link.I used this library to add long click listeners on a auto link.I implemented it successfully.But when I do this, i want to show a Lottie animation in the start like this but on long click of a link.
I tried many answer but I get an exception.I already made the layout for custom snakbar.It is given below
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="56dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="#dimen/_3sdp"
android:backgroundTint="#color/snakbar_background"
app:cardCornerRadius="#dimen/_4sdp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/lottieSnakbar"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginStart="#dimen/_5sdp"
android:layout_marginEnd="#dimen/_10sdp"
app:lottie_rawRes="#drawable/copy"
app:lottie_autoPlay="true"
app:lottie_loop="false"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Text copied to clipboard"
android:gravity="center_vertical"
android:theme="?snackbarTextViewStyle"
tools:ignore="HardcodedText" />
</LinearLayout>
</androidx.cardview.widget.CardView>
Now how can I achieve this?
You can use an interface in the adapter. And receive and show snakbar from activity
You could use localBroadcast and broadcast a local signal from the adapter and let the other Activity(probably receiver) handle snakbar.
AdapterClass{
LocalBroadcastManager localBroadcastManager = null;
void sendB(parms){
if(localBroadcastManager != null){
localBroadcastManager.sendBroadcast(Intent("NOTIFICATION_RECEIVER"))
}
onCreateViewHolder(){
localBroadcastManager = LocalBroadcastManager.getInstance(applicationContext)
}
onBindViewHolder(){
if(someCondition){
sendB(parms);
}
}
then receive it from an activity then call snakBar

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.

Custom Facebook Share-Button, Android App

So I want to implement a custom Facebook share button into my Android app, but thus far I've only managed to use the native one, which I imported into my .xml file and made use of in my Java activity for that specific page.
My code looks like this (in .xml);
<com.facebook.share.widget.ShareButton
android:id="#+id/share_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Share"
android:layout_centerHorizontal="true"/>
And in my .java, outside onCreate();
private ShareButton shareButton;
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse("https://developers.facebook.com"))
.setContentTitle("MyTitle")
.build();
Inside onCreate();
shareButton = (ShareButton)findViewById(R.id.share_btn);
shareButton.setShareContent(content);
How would I go about making use of a custom button that I've imported into XML? Using .setShareContent obviously doesn't work if it's not an instance of ShareButton. Thanks in advance!
I have found a solution. It's clear that facebook share will work on its own button click event. So you will have to explicitly call the View.performclick() method.
Here is how i have done this:
In my layout
<com.facebook.share.widget.ShareButton
android:id="#+id/share_btn_fb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:contentDescription="#string/share" />
<LinearLayout
android:id="#+id/share_btn"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:src="#drawable/google_share" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/share_google"
android:textColor="#color/green_color"
android:layout_marginLeft="5dp"
android:textSize="18sp" />
</LinearLayout>
Inside activity get refrences of these views.
ShareButton shareButton = (ShareButton) layout.findViewById(R.id.share_btn_fb);
LinearLayout shareFB = (LinearLayout) layout.findViewById(R.id.share_btn);
LinearLayout shareGoogle = (LinearLayout) layout.findViewById(R.id.share_google);
shareGoogle.setOnClickListener(this);
shareFB.setOnClickListener(this);
Inside onClick()
case R.id.share_btn :
shareButton.performClick();
break;
case R.id.share_btn_fb :
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentTitle("Content title")
.setContentDescription("App descr")
.setContentUrl(Uri.parse("some url"))
.build();
shareButton.setShareContent(content);
break;
Basically shareButton.performClick(); is doing the trick.

How to fix the layout in OnConfigurationChanged when i do not want the activity to restart

So i fixed my activity restart on changing orientation (i needed to do that for client requirement) but now i am having issues fixing the display layouts.I have tried a lot of combinations of layout_height and layout_weight but failed.
I just need some guidance in how to fix this.
My layout xml:
<?xml version="1.0" encoding="UTF-8"?>
<!--
/*
Session screen layout
Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/session_root_view"
android:background="#F7FE2E"
>
<!-- childs are specified bottom-up so that we have a correct z-order in our final layout -->
<android.inputmethodservice.KeyboardView
android:id="#+id/extended_keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="gone"
/>
<com.freerdp.freerdpcore.presentation.ScrollView2D
android:id="#+id/sessionScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/extended_keyboard"
android:scrollbars="horizontal|vertical"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:background="#FF0040"
android:drawingCacheQuality="high"
android:isScrollContainer="true"
>
<com.freerdp.freerdpcore.presentation.SessionView
android:id="#+id/sessionView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:drawingCacheQuality="high"
/>
</com.freerdp.freerdpcore.presentation.ScrollView2D>
<com.freerdp.freerdpcore.presentation.TouchPointerView
android:id="#+id/touchPointerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/touch_pointer_default"
android:visibility="invisible"
/>
<android.widget.ZoomControls
android:id="#+id/zoomControls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/sessionScrollView"
android:layout_centerHorizontal="true"
/>
<android.inputmethodservice.KeyboardView
android:id="#+id/extended_keyboard_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:visibility="gone"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
The relevant code which i use in onConfigurationChanged:
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT);
scrollView.setLayoutParams(params);
sessionView.setLayoutParams(new FrameLayout.LayoutParams(android.widget.FrameLayout.LayoutParams.MATCH_PARENT,
android.widget.FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));
scrollView.requestLayout();
sessionView.requestLayout();
Screenshot when initially in Portrait:
Screenshot when in landscape:
thanks.
Try this
LayoutParams p = scrollView.getLayoutParams();
LayoutParams p1 = sessionView.getLayoutParams();
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
p.width = outMetrics.widthPixels;
p.height = outMetrics.heightPixels
p1.width = outMetrics.widthPixels;
p1.height = outMetrics.heightPixels
your view may be filling the screen with fill_parent. But the portrait aspect ratio image will not fit in as background in landscape layout.
use a different image of landscape width and height and use it in drawable-land folder (name of the image should be same)
image from drawable-land will be automatically picked for landscape layout

"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.

Categories