Can't add Fragment to Activity onClick on a button - android

I'm trying to make a fragment appear next to my activity view when I click on a button, but I keep having this error and I can't solve this.
java.lang.IllegalArgumentException: No view found for id 0x7f090081 (nofix.oc:id/register_form_container) for fragment FragmentRegisterClient{6a297ba #0 id=0x7f090081}
I took a screenshot to explain you what I want to happen : http://prntscr.com/i6dx9f
Here is the code of the Activity I'm calling my Fragment from :
public class RegisterActivity extends AppCompatActivity{
[...]
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tmpSavedInstanceState = savedInstanceState;
setContentView(R.layout.activity_register);
[...]
FrameLayout frame = new FrameLayout(this);
frame.setId(CONTENT_VIEW_ID);
setContentView(frame, new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.MATCH_PARENT));
if (tmpSavedInstanceState == null) {
Fragment newFragment = new FragmentRegisterClient();
Log.e("error","eezrzezeerez");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.register_form_container, newFragment).commit();
}
Here is the layout f RegisterActivity:
<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_horizontal"
android:orientation="vertical"
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="nofix.Links.RegisterActivity">
<!-- Login progress -->
<ProgressBar
android:id="#+id/register_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/register_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/email_register_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:layout_marginBottom="15dp">
<TextView
android:id="#+id/textView"
android:layout_width="104dp"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_weight="0.80"
android:text="#string/account_type"
android:textSize="20sp"
app:layout_constraintRight_toLeftOf="#+id/dropdown_account_type"
tools:layout_editor_absoluteY="8dp" />
<Spinner
android:id="#+id/dropdown_account_type"
android:layout_width="179dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="157dp"
android:layout_weight="0.67" />
</LinearLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/name_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_name"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_register"
android:imeOptions="actionUnspecified"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/first_name_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_first_name"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_register"
android:imeOptions="actionUnspecified"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="#+id/email_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_email"
android:inputType="textEmailAddress"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/prompt_password"
android:imeActionId="#+id/login"
android:imeActionLabel="#string/action_register"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/email_register_form_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/action_register"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/register_form_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
And here is code and XML of my fragment :
public class FragmentRegisterClient extends Fragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.e("First test", "Please tell me it's ok");
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_register_client, container, false);
Log.e("Second Test", "NICE");
return view;
}
}
Here is the XML
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
tools:context="nofix.Links.FragmentRegisterClient">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bite"/>
</FrameLayout>
EDIT:
I had to remove the second setContentView() to make it work.

Related

Dynamically added EditText is not visible when created

I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).
I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.
My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.
The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<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:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">
<android.support.design.widget.TextInputEditText
android:id="#+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/teamName"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="#android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="#android:color/background_light"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="#android:color/background_light" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the AddTeamMembers Class that calls the above XML
public class AddTeamMembers extends Fragment implements
View.OnClickListener
{
private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;
//The below method must be overridden in order to implement a fragment -
this changes the lifecycle method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_team_members,
container, false);
initViews(view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 0, Sets Shared pref mode to private
pref = getActivity().getPreferences(0);
tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));
}
private void initViews(View view) {
tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);
mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);
tv_add_name.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_name:
createEditTextView();
break;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void createEditTextView() {
try{
//dynamically create new EditText when user clicks to add another
name
//target user using API 22 and above (lollipop and above)
EditText editTextView = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.M) {
editTextView = new EditText(getContext());
}else{
Toast.makeText(getActivity(), "App is not supported on this
device",
Toast.LENGTH_LONG).show();
}
editTextView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
mLayout.addView(editTextView);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
}
[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]
[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png

Fragment is not overlapping buttons that are in the activity

I made a progress-bar fragment; the goal is to show it whenever the buttons in my MainActivity are clicked. However, I encountered this dilemma (using Android 5.1):
https://gyazo.com/8632f6e50cdce599847258939cc4f109
Any ideas what's the deal here? I can't pin-point the problem. The only solution is to hide the button when clicked. It works but I interpret is as badly-written code.
Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
final Button bRegister = (Button) findViewById(R.id.bRegister);
final Button bTest = (Button) findViewById(R.id.bTest);
bTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.activity_register, progressFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
ProressFragment
public class ProgressFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_progress, container, false);
ProgressBar pb = (ProgressBar) view.findViewById(R.id.progressBar);
pb.setVisibility(View.VISIBLE);
return view;
}
}
XML for MainActivity (Looks like this:https://gyazo.com/8ecef7ada74cf92730b4d99757383da7)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_register"
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="googleplayservices.samples.android.teamtreehouse.com.kibbleuserc.RegisterActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Password"
android:ems="10"
android:id="#+id/etPassword"
android:layout_below="#+id/etEmail"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/etEmail"
android:layout_alignEnd="#+id/etEmail"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Phone Number"
android:ems="10"
android:id="#+id/etPhoneNum"
android:layout_below="#+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/etPassword"
android:layout_alignEnd="#+id/etPassword"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/etName"
android:layout_below="#+id/etPhoneNum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/etPhoneNum"
android:layout_alignEnd="#+id/etPhoneNum"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Email"
android:ems="10"
android:layout_marginTop="10dp"
android:id="#+id/etEmail"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/etEmail"
android:id="#+id/tvEmailError"
android:layout_above="#+id/etPassword"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/etEmail"
android:textColor="#FFFF4444"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPasswordError"
android:layout_alignBottom="#+id/etPassword"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/etPassword"
android:layout_toEndOf="#+id/etPassword"
android:layout_below="#+id/etEmail"
android:textColor="#FFFF4444"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/etPhoneNum"
android:layout_toRightOf="#+id/etPhoneNum"
android:id="#+id/tvPhoneNumError"
android:layout_below="#+id/etPassword"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#FFFF4444"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/etName"
android:layout_toRightOf="#+id/etName"
android:id="#+id/tvNameError"
android:layout_below="#+id/etPhoneNum"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#FFFF4444"/>
<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bRegister"
android:layout_below="#+id/etName"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bRegister"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="31dp"
android:id="#+id/bTest"/>
</RelativeLayout>
XML for ProgressFragment (Looks like this:https://gyazo.com/93bbb11af8cb8a0df8d4d6cf97b84ef1)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
It happens because the two buttons have an elevation that is not 0 while your RelativeLayout in your Fragment have an elevation of 0.
You can fix it wrapping the RelativeLayout of the Activity in a FrameLayout, like this:
<?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:id="#+id/activity_register"
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="googleplayservices.samples.android.teamtreehouse.com.kibbleuserc.RegisterActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Add all the views that you have inserted in your RelativeLayout -->
</RelativeLayout>
</FrameLayout>
However the optimized solution is to set the elevation in your Fragment's root View with ViewCompat.setElevation(View,int) but you have to know the right elevation to set to bring the View on top of the others. In this case you won't need the FrameLayout wrapper and the onDraw() and onLayout() calls will be optimized.

OnClickListener and fragments causing application crash

I'm trying to use fragments to programatically change the text on a screen. To do this I'm setting up an on click listener on a text view and then if it's clicked starting a fragment manager, replacing the current fragment with the new fragment. However, this causes my app to crash when it's started.
From reading the crash report it seems like the error is happening at tv1.setOnClickLIstener...
Finally, Android Studio keeps giving me a type mismatch when I use fragment or support fragment. That is why you see android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Java Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//begin transaction
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//replace the contents of the container with the new fragment
ft.replace(R.id.placeHolder, new SplashScreenFragment());
ft.commit();
TextView tv1 = (TextView) findViewById(R.id.whatIsHumanTrafficing);
tv1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.placeHolder, new whatIsHumanTrafficing());
ft.commit();
}
});
}
}
XML from activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/placeHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
XML displayed before click:
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.piatt.worksafe.MainActivity"
android:id="#+id/splashScreenId"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Safe!"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Human Trafficing?"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/whatIsHumanTrafficing"
android:clickable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I get safe labor?"
android:layout_below="#+id/whatIsHumanTrafficing"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/howDoIGetSafeLabor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I check that my job / job offer is legal?"
android:layout_below="#+id/howDoIGetSafeLabor"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"
android:id="#+id/checkLegality"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How can I get help?"
android:layout_below="#+id/checkLegality"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/getHelp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:layout_below="#+id/getHelp"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/aboutUs"
/>
</RelativeLayout>
XML to be displayed after click on text view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Human Trafficing Is:"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confiscation of travel documents"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HTdescription1"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unregistered Labor"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription2"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Isolation from friends and family"
android:layout_below="#+id/HTdescription2"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription3"
android:layout_gravity="center"
/>
</LinearLayout>
Fragment named human trafficking that I'm trying to inflate:
public class whatIsHumanTrafficing extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
//inflate the layout for this fragment
return inflater.inflate(R.layout.what_is_human_trafficing, container, false);
}
}
You have used activity_main in your MainActivity as follows
setContentView(R.layout.activity_main);
and you are accessing whatIsHumanTrafficingnamed TextView in the same MainActivity which is not possible as it is not present in activity_main and which is present in another xml. So you are getting NullPointerException there.

Getting NullPointerException when adding toolbar

Getting NullPointerExceptionwhen trying to add a toolbar which is been defined in a layout file other than main design xml file. I keep getting the following error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.Toolbar.findViewById(int)' on a null object reference
Here is my code
public class History_List_Details_Fragment extends Fragment {
Histroy_List_Model histroyListModel;
public History_List_Details_Fragment() {
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
histroyListModel = getArguments().getParcelable("historydetail");
String title=histroyListModel.getShared_title();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
View rootView = inflater.inflate(R.layout.history_list_details_fragment, container, false);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.app_bar);
initializeListnersForToolbar(toolbar);
initializeViews(rootView);
return rootView;
}
private void initializeViews(View rootView) {
TextView titleText = (TextView) rootView.findViewById(R.id.txtTitle);
titleText.setText(histroyListModel.getShared_title());
TextView messageTExt = (TextView) rootView.findViewById(R.id.txtMessage);
titleText.setText(histroyListModel.getShared_message());
}
private void initializeListnersForToolbar(Toolbar toolbar){
((TextView) toolbar.findViewById(R.id.tool_bar_text_head)).setText(getResources().getString(R.string.friends));
((ImageView) toolbar.findViewById(R.id.tool_bar_back_arrow)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment fragment=new History_List_Fragment();
Utilities.getInstance(getActivity()).change_HomPage_Fragment(
fragment, "History_List_Fragment", getActivity());
}
});
}
}
History_List_Details_Fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="#+id/txtTitle"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="#+id/txtMessage"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Duration"
android:id="#+id/txtDuration"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/lstMembers"></ListView>
</LinearLayout>
</LinearLayout>
How can I fix this error?
Add Toolbar in "history_list_details_fragment" with id "app_bar"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="68dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title"
android:id="#+id/txtTitle"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Message"
android:id="#+id/txtMessage"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Duration"
android:id="#+id/txtDuration"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/lstMembers"></ListView>
</LinearLayout>
The error states clearly. Inside history_list_details_fragment.xml there is no Toolbar. Try to add it.
Or more probably the Toolbar is in you Activity layout, so init it inside Activity.

onbuttonclick display a hidden fragment

When I click on login button, it says that app is stopped working. But It should be changed to fragment register. Can anyone tell what I should change in following java file ? because I am new for Android, Please kindly help.
**AdminFragment.java**
public class AdminFragment extends Fragment implementsView.OnClickListener {
EditText etUsername,etPassword;
Button bLogin;
public View mRootView;
public AdminFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView = inflater.inflate(R.layout.fragment_admin, container, false);
etUsername=(EditText)mRootView.findViewById(R.id.etUsername);
etPassword=(EditText)mRootView.findViewById(R.id.etPassword);
bLogin=(Button)mRootView.findViewById(R.id.bLogin);
bLogin.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
Fragment fragment=null;
switch (v.getId()){
case R.id.bLogin:
fragment = new RegisterFragment();
replaceFragment(fragment);
break;
}
}
private void replaceFragment(Fragment AdminFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab,AdminFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
enter code here
my Admin Xml file
My xml file in admin fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.AdminFragment">
<TextView
android:id="#+id/tvUsername"
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tvPassword"
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/etPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bLogin"
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</LinearLayout>
My Register java file
public class RegisterFragment extends Fragment implementsView.OnClickListener {
Button bRegister;
EditText etHospital;
public View mRootView;
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mRootView= inflater.inflate(R.layout.fragment_register, container, false);
bRegister=(Button)mRootView.findViewById(R.id.bRegister);
etHospital=(EditText)mRootView.findViewById(R.id.etHospital);
bRegister.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bRegister:
break;
}
}
}
My register xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schppemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:layout_height="match_parent"
tools:context="com.swach.wecareapp.fragments.RegisterFragment">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="#string/Hospital_Name"
/>
<EditText
android:id="#+id/etHospital"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="City"
/>
<EditText
android:id="#+id/etCity"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Level"
/>
<EditText
android:id="#+id/etLevel"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Complaint_Address"
/>
<EditText
android:id="#+id/etAddress"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Phone"
/>
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Street"
/>
<EditText
android:id="#+id/etStreet"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bRegister"
android:text="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

Categories

Resources