I want to display a Progressbar in my LoginFragment for testing purposes however after I connect my view variables in the OnCreateView Callback in my LoginFragment, my app shuts down and its due to:
Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
But what's driving me nuts is that this is the only View that throws a NPE, all my other views I am able to manipulate and reference without a problem. I also tried to use a Progressbar in the LoginActivity that hosts the LoginFragment and the Progressbar worked as expected, but why not in the fragment ??
I attached the code xml and fragment code corresponding to the LoginFragment
public class LoginFragment extends Fragment {
private AutoCompleteTextView name;
private EditText password;
private ProgressBar progressBar;
//......... Skipped For Brevity .........
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.login_fragment, container, false);
v.setBackgroundResource(R.drawable.album_jazz_blues);
name = v.findViewById(R.id.username);
password = v.findViewById(R.id.password);
progressBar = v.findViewById(R.id.progress_bar);
//progressBar.setVisibility(View.VISIBLE); <-------- THROWS NPE
return v;
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:id="#+id/constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/login_parent_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:padding="16dp"
android:orientation="vertical"
tools:layout_alignParentTop="true"
tools:layout_alignParentStart="true" >
<AutoCompleteTextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="#string/email"
android:maxLines="1"
android:singleLine="true" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
tools:ignore="Autofill" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<ProgressBar
style="#style/Widget.AppCompat.ProgressBar"
android:layout_gravity="center"
android:indeterminate="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progress_bar">
</ProgressBar>
What Am I missing here ?
As Aaron in the comments pointed out, I had two different layout folders a normal folder and v-26 folder which did not necessarily mirror each other so what I did was assign the folders the exact same elements and attributes with their corresponding values with the exception of
EditTextandroid:importantForAutofill
autofill attribute, in v-26 android:importantForAutofill="no and in my regular layout folder tools:ignore="Autofill"
Related
I have created a tabLayout that swipes between two fragments using a viewPager2 + Fragment State Adapter
Within Fragment 2, I have a TextClock within a scroll view.
When I scroll down to the bottom of the page, it keeps jumping to the top if the page
Video link showing issue
Any help would be appreciated
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".BarcodeFragment">
Xml Code:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="104dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextClock
android:id="#+id/simpleTextClock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:format24Hour="HH : mm : ss"
android:textColor="#color/colorDark"
android:textScaleX=".94"
android:textSize="18sp"
android:textStyle="bold"
app:fontFamily="#font/tlcircular_bold"
app:layout_constraintBottom_toTopOf="#+id/box02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/box02"
android:layout_width="329dp"
android:layout_height="283.99986dp"
android:layout_marginTop="450dp"
android:background="#drawable/box_rounded_ticket"
android:shadowColor="#00FF0A0A"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Java Code:
</**
* A simple {#link Fragment} subclass.
*/
public class TicketFragment extends Fragment {
public TicketFragment() {
// Required empty public constructor
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_ticket, container, false);
return rootView;
}
}>
SOLVED
Whenever configuring the height / width of a TextClock - never use 'wrap_content'. I had to reverse engineer my entire app to find such such a simple problem.
My question is: why has this never been flagged before?!
I just want to ask you how can i use webview with an entered strings from edittext?
For example, i created 2 fragment one of these shows webview and another one gets 2 edittext from users. I just want to get entered edittext values from 2. fragment to 1. fragment. Because i want create a URL like this : webView.loadUrl("http://www.truebilisim.com/myiphone/index.php?id=" + ID + "&sifre=" + Password);
This is my first fragment.
<FrameLayout 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"
tools:context=".Tab1">
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And this is the second one.
<FrameLayout 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"
tools:context=".Tab2">
<RelativeLayout 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:paddingTop="80dp"
android:paddingRight="30dp"
android:paddingLeft="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tab2">
<ImageView
android:src="#drawable/logo_small"
android:id="#+id/logo"
android:layout_marginBottom="30dp"
android:layout_width="match_parent"
android:layout_height="100dp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/layout_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/logo">
<EditText
android:hint="ID"
android:textColor="#color/colorPrimary"
android:id="#+id/idx"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/layout_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/layout_id"
>
<EditText
android:inputType="textPassword"
android:hint="Şifre"
android:textColor="#color/colorPrimary"
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_login"
android:layout_below="#id/layout_pass"
android:backgroundTint="#color/colorPrimaryDark"
android:text="KAYDET"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Your question is too big to answer here. And its not specific. I'm just going to mention the steps. You'll find code for specific parts if you search properly.
Initialize your EditTexts. You you'll send the values from the EditText's when the user clicks the login button.
Set onClickListener on the login button. Inside the listener, check if your EditTexts have value or not. If they have value, goto the other fragment.
Send the values of EditText to the other fragment, you can do this in multiple ways. One is, just get an instance to the destination fragment object, and send the value.
I get these strings like this but cant find a way to use at first fragment. Can u show me ?
public class Tab2 extends Fragment {
public Tab2() {
// Required empty public constructor
}
EditText ed1,ed2;
private Button b1;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab2, container, false);
b1 = view.findViewById(R.id.btn_login);
ed1 = view.findViewById(R.id.idx);
ed2 = view.findViewById(R.id.password);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String n = ed1.getText().toString();
String p = ed2.getText().toString();
}
});
return view;
}
}
When I initialize ListView inside OnActivityCreated() method of a class that extends Fragment as given below :
ListView listView=(ListView)getActivity().findViewById(R.id.list);
then it works fine but when i do same thing for the TextView i.e initialize textView in same way inside the same method of class that extends Fragment
textView = (TextView)getActivity().findViewById(R.id.textView);
then my app crashes and log shows null pointer exception . I want to know the reason behind this behavior . !! Xml Code is Given Below :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:text="some tect"
android:textSize="28sp" />
</RelativeLayout>
What you are doing is wrong.
Just do
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ListView listView=(ListView)view.findViewById(R.id.list);
textView = (TextView)view.findViewById(R.id.textView);
}
Initialize all view as above.
I've implemented onClickListener but inside onCreateView() it's unable to find the button's id and i.e "myButton"
My Code is:
public class Category extends Fragment implements View.OnClickListener {
View view;Button b;
public Category() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_category, container,false);
b=(Button)view.findViewById(R.id.myButton);
b.setOnClickListener(this);
return view;
}
My fragment_category.xml is as below it's containing one button and a textView:
<RelativeLayout 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"
tools:context="layout.Category"
android:id="#+id/constraintLayout">
<TextView
android:layout_width="368dp"
android:layout_height="495dp"
android:text="#string/hello_blank_fragment"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:elevation="0dp"
android:text="Button"
tools:layout_editor_absoluteX="135dp"
tools:layout_editor_absoluteY="208dp" />
You are inflating "fragment_category", but you said you posted "fragment" xml code.
You're most likely not inflating the correct layout file.
Actually problem was there in IDE after some rectifications the error is sorted out.
Been trying to figure this out for a while now:
I'm trying to create a custom dialog using the standard approach shown on the Dev site.
public class CustomDialog extends DialogFragment {
private OnDialogResultListener mOnDialogResultListener = null;
public void setOnDialogResultListener(OnDialogResultListener dialogResultListener) {
mOnDialogResultListener = dialogResultListener;
}
public static CustomDialog newInstance(OnDialogResultListener dialogResultListener) {
CustomDialog frag = new CustomDialog();
frag.setOnDialogResultListener(dialogResultListener);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle(getString(R.string.Dialog_CustomDialog_Title));
View v = inflater.inflate(R.layout.customdialog, container, false);
return v;
}
}
With the XML being:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent" android:background="#color/white" android:layout_margin="0px" android:layout_width="fill_parent">
<EditText android:hint="#string/Dialog.CustomDialog.OldPassword" android:layout_marginBottom="#dimen/normalMargin" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/EditText02" android:layout_width="fill_parent"></EditText>
<EditText android:hint="#string/Dialog.CustomDialog.NewPassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/EditText01" android:layout_width="fill_parent"></EditText>
<EditText android:hint="#string/Dialog.CustomDialog.RetypePassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/editText1" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<Button android:layout_height="wrap_content" android:text="#string/save" android:layout_width="#dimen/normalButtonWidth" android:id="#+id/btn_Custom_save"></Button>
<Button android:layout_height="wrap_content" android:text="#string/cancel" android:layout_width="#dimen/normalButtonWidth" android:id="#+id/btn_Custom_cancel"></Button>
</LinearLayout>
</LinearLayout>
And after creating and showing the dialog, I'm left with:
The white background has been applied to emphasize the unexpected and unwanted behavior.
Any ideas? I've tried changing width/heights, using weights in a horizontal LinearLayout, setting width/height programatically - all to no avail.
I found this tricks:
getDialog().getWindow().setBackgroundDrawableResource(R.color.white);
Looks like better solution.
I did come up with a cheap fix, which I would rather not have to use.
Adding a 0px height, 2000dp (some huge number) width view anywhere in the layout causes it to fill the dialog frame.
With the DialogFragment the width and height are null. Create an extra sub view group with fill parent and it should work for you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_height="fill_parent" android:background="#color/white" android:layout_margin="0px" android:layout_width="fill_parent">
.....
If you set attachToRoot = true during inflater.inflate it should show dialog correctly.
View v = inflater.inflate(R.layout.customdialog, container, true);
I used a relativeLayout as the root view in my case and it actually filled out the dialog windows.