I am trying to create edittext using Java in bottom fragment class based on input that is to be passed from top fragment. But when I type
Button add_submit = new Button(this);
I get error for the this for parameter. However I can use this code in MainActivity.java.
Why is this so? What is causing the error and how to fix it?
The following are the complete code for the class
package com.test.gpacalc;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class AddActivityBottom extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_bottom,container,false);
return view;
}
public void createAddInput(String number_of_subjects){
Button add_submit = new Button(this);
}
}
Button constructor requires context as an argument. Fragment doesn't implement it, activity does. try new Button(getActivity())
Related
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
I just started to work in android studio. Tutorials are helping and all, but I can't find any help for this problem (it might be obvious, but I don't see it).
My problem comes after implementing the communication between the fragment activity and main activity.
Error after compiling is:
error: incompatible types: FragmentActivity cannot be converted to MainActivity
After going to where the error is, it says:
Incompatible types.
Required: com.app.appname.mainactivity
Found: androidx.fragment.app.FragmentActivity
This is the code for my fragment, where the error happens:
(names are in my language)
package com.ors.herobomb;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Uspeh extends Fragment {
EditText upis;
Button poslji;
public Uspeh() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View p = inflater.inflate(R.layout.fragment_uspeh, container, false);
upis = p.findViewById(R.id.hero);
poslji = p.findViewById(R.id.poslji);
poslji.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String ode = upis.getText().toString();
if(ode.length()>1){
MainActivity main = getActivity();
main.send(ode);
}
}
});
return p;
}
}
Answer provided by Daniel.
Replacing:
MainActivity main = getActivity();
With:
MainActivity main = (MainActivity) getActivity();
Solved my problem.
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();
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.
I have created project in Android Studio with Navigation Drawer Activity and I have successfully created it,On navigation drawer activity I have created 3 section, In 1st section I put Button and I want to setOnClickListener method on that button which lead to start new Activity (ex. "xyz.class") I have used code
startActivity(new Intent(this,xyz.class));
but "this" keyword is not working and gives me error.
So, I changed code like
Context c;
startActivity(new Intent(c,xyz.class));
which gives NullPointerException,
My Section1 code is
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.pratik.crm50.R;
import com.gc.materialdesign.views.ButtonFloat;
public class Dashboard extends Fragment implements View.OnClickListener {
View rootView;
Context c;
private ButtonFloat float_btn;
private Button but;
private Button btn;
Context c;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View v;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
v = inflater.inflate(R.layout.dashboard_layout,container, false);
} else {
v = super.onCreateView(inflater,container, savedInstanceState);
}
View vv=v.findViewById(R.id.button_simple);
vv.setOnClickListener(this);
return v;
}
#Override
public void onClick(View v) {
// Toast.makeText(c,"Float Button",Toast.LENGTH_SHORT).show();
Log.e("ssas","sasafa");
//startActivity(new Intent(c,xyz.class));
}
}
and I can get successfully log Log.e("ssas","sasafa") on above code.
So how to do this?
Fragment does not extend Context while the Intent constructor expects the first parameter to be exactly that.
The Context c variable is redundant because a Fragment saves the parent activity by default.
You need to use the activity (class that extends Context) that the fragment is attached to:
startActivity(new Intent(getActivity(), xyz.class));
You get a NullPointerException, because you´re never assining a value to Context c.
So as user3249477 also mentioned use the method getActivity() to assign a value to c like c=getActivity();
Or call it directly while starting the Intent. This would look like this: startActivity(new Intent(getActivity(),xyz.class));