Toolbar in Fragment crashes app on launch - java

This seems like a common issue with lots of accepted answers but none of them seem to be working for me.
Most answers suggest using this piece of code to add a toolbar into a fragment:
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
Alas, this still crashes my app even though I'm using AppCompatActivity. I think the error may lie in the fact that my Fragment class extends Fragment and not AppCompatActivity but I don't know enough about android as of yet to be sure of this. It crashes on the setSupportActionBar line.
My Fragment code:
package erikligai.ribbitapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by erik on 2017-06-07.
*/
public class MessageFragment extends Fragment {
Toolbar mToolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// return inflater.inflate(R.layout.message_fragment_layout, container, false);
View rootView = inflater.inflate(R.layout.message_fragment_layout, container, false);
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
return rootView;
}
Would appreciate any suggestions.

You need to cast your activity from getActivity() to AppCompatActivity
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle();
Toolbar getting from Activity, not from Fragment
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);

Related

OnClickListener doesn't work in a fragment

I have here a problem with the onClicklistener method in a fragment. If I click on the button nothing happens and I don't know why. No error and no output.
I've tried to implements the OnClickListener or set a (Button) before the view.findViewById(R.id.button); but nothing helps.
I've seen much questions here in Stack Overflow about this problem but no solution from there helps me :/
Do you have any ideas? Thank You!
package com.christoph.myapplication.ui.home;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.christoph.myapplication.R;
public class HomeFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
final Button button= view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setBackgroundColor(Color.RED);
System.out.println("Hallo");
}
});
return view;
}
}
You can add click listener rather in onViewCreated function. Try it and let me know if it works. Check android fragment lifecycles documentation for more info.
Cheers

How to call dialogfragment from another fragment?

I'm really new in android development, hope you guys can help me in my problem. I had already search for any solution but none of it works.
I have 6 fragments for scrollable tabs, then each tabs has ADD TO CART buttons, if I click that button a fragment dialog should appear. In my case, there is this error.
Error:(37, 13) error: no suitable method found for show(android.support.v4.app.FragmentManager,String)
method DialogFragment.show(android.app.FragmentManager,String) is not applicable
(argument mismatch; android.support.v4.app.FragmentManager cannot be converted to android.app.FragmentManager)
method DialogFragment.show(FragmentTransaction,String) is not applicable
(argument mismatch; android.support.v4.app.FragmentManager cannot be converted to FragmentTransaction)
Heres my code by the way for the fragment to call the DialogFragment.
package info.androidhive.materialtabs.fragments;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import info.androidhive.materialtabs.R;
public class OneFragment extends Fragment{
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);
}
public void toDiagCartFragment(View v){
FragmentManager manager = getFragmentManager();
CartFragment cart = new CartFragment();
cart.show(manager, "My Cart");
}
}
This the code for DialogFragment to be called by OneFragment
package info.androidhive.materialtabs.fragments;
import android.app.DialogFragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import info.androidhive.materialtabs.R;
public class CartFragment extends DialogFragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_cart2, null);
}
}
Seems like the error is in the OneFragment.java
Heres my output by the way.
_I really appreciate for any answers, just please be nice to me
.I really don't know how to do it . :(
.Thanks :)
your error says your answer clearly.
type mismatch
your dialogFragment has android.app.FragmentManager and you are calling android.support.v4.app.FragmentManager. you should use getSupportFragmentManger() insted of getFragmentManager();
first of all replace your cartFragment import to this,
import android.support.v4.app.DialogFragment;
and create this method.
public static CartFragment newInstance() {
CartFragment dialog = new CartFragment ();
return dialog;
}
and in your one_fragment use like this.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
CartFragment newFragment = CartFragment .newInstance();
newFragment.show(ft, "My Cart");
your OneFragment belongs to support library fragment while CartFragment doesn't. you need to either modify CartFragment to support library or OneFragment to default Fragment. and modifying CartFragment to support library fragment will be better as support library has more functionality.
There is a version mismatch. Instead of using getFragmentManager(), use getSupportFragmentManager() as you are using the support library version of Fragments.
Replace
FragmentManager manager = getFragmentManager();
with
FragmentManager manager = getSupportFragmentManager();

Different behavior of android.app.Fragment and android.support.v4.app.Fragment while using backstack

I've created Activity and added a fragment to it using FragmentManeger. When i use android.app.Fragment and press the back button my application closes. When i use android.support.v4.app.Fragment and press the back button, the fragment is removed from the activity, but application is still working. I can't really understand why is that happening.
Here is the code i used:
Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getFragmentManager().beginTransaction()
.replace(R.id.content_fragment, new Fragment1())
.addToBackStack("first")
.commit();
}
}
Fragment:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
public Fragment1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
When i simply replace import in Activity and Fragment to same classes, but in the support library, the result is different...
EDIT:
I also replaced getFragemetManeger() to getSupportFragmentMeneger() and it still works different
if you're only looking to change the back behavior, you can try overriding
onBackPressed with something like:
if(backstackCount() > 0)
{
super.onBackPressed
}
The issue occured because i used android.support.v7.app.AppCompatActivity and android.app.Fragment. I should've used android.app.Fragment with android.app.Activity or AppCompatActivity with support fragment.

NavigationDrawer's menu is not working

When I click at the menu items, then layouts not coming to view.
And MainActicity wanna "menu1_Fragment.java" encode with "android.support.v4.app.Fragment"
İf I encode only "Fragment", MainActivity is getting error.
In compatible types.
Required :android.support.v4.app.Fragment
Found :intizamyazilim.navigationdrawernew.menu1_Fragment
Here is menu1_Fragment.java
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 01.03.2015.
*/
public class menu1_Fragment extends android.support.v4.app.Fragment {
View rootview;
#Nullable
// #Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
}
I Fix it.
I changed this "menu1_Fragment.java" with this codes:
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class menu1_Fragment extends android.support.v4.app.Fragment {
public menu1_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu1_layout, container, false);
return rootView;
}
}
No it is not the proper way to fix it. The proble here is that you are extending your menu1_Fragment using support library and on the other hand you are importing fragment from SDK(import android.app.Fragment;). Make it consistent. Either use fragment from support or from android SDK.

Constructor within activity is undefined

I have been fighting with this error for a couple of hours and I am not able to continue. I need your help here!
I am trying to replace my previous TabNavigation at ActionBar by a ViewPager in SDK21, looking at comments within StackOverflow I found this webpage, where the use of PagerTabStrip is described with an example, so I tried to implement it in my activity, however I am getting an strange error.
I tried to google the problem and all the suggestions are not really applicable to my problem (wrong parameters in the constructor are the usual errors) . I also reproduced the error with a simple Activity in order to avoid anything I have in my previous activity, but the error keeps. I attach you here the code I replicated isolated:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class TestActivity extends Activity {
CustomPagerAdapter mCustomPagerAdapter;
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adding_users_to_list);
mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this.getApplicationContext());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCustomPagerAdapter);
}
class CustomPagerAdapter extends FragmentPagerAdapter {
Context mContext;
public CustomPagerAdapter(FragmentManager fm, Context context) {
super(fm);
mContext = context;
}
#Override
public Fragment getItem(int position) {
// Create fragment object
Fragment fragment = new DemoFragment();
// Attach some data to the fragment
// that we'll use to populate our fragment layouts
Bundle args = new Bundle();
args.putInt("page_position", position + 1);
// Set the arguments on the fragment
// that will be fetched in the
// DemoFragment#onCreateView
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return "Page " + (position + 1);
}
}
class DemoFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout resource that'll be returned
View rootView = inflater.inflate(R.layout.fragment_users, container, false);
// Get the arguments that was supplied when
// the fragment was instantiated in the
// CustomPagerAdapter
Bundle args = getArguments();
((TextView) rootView.findViewById(R.id.text_option)).setText("Page " + args.getInt("page_position"));
return rootView;
}
}
}
I am getting in the call to the constructor the following error:
"The constructor TestActivity.CustomePageAdapter(FragmentManager, Context) is undefined"
In here:
mCustomPagerAdapter = new CustomPagerAdapter(getFragmentManager(), this.getApplicationContext());
I have tried to introduce the Adapter outside and inside the activity definition and is still not working. Is likely to be something simple, but... I can't see it and I need other eyes. Any idea what I am doing wrong?
change the import of the activity to:
import android.support.v4.app.FragmentActivity;
Also change constructor to:
mCustomPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), this.getApplicationContext());
The reason is that your activity is used from the ADT:
import android.app.Activity;
and the Fragments used from the support package:
import android.support.v4.app.FragmentManager;
change the import of the activity to:
import android.support.v4.app.FragmentActivity;

Categories