Required: com.app.appname.mainactivity, Found: androidx.fragment.app.FragmentActivity - java

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.

Related

Cannot create widget in Android Fragment

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())

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.

Open activity on button click in NavigationDrawer

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));

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;

Android Fragment Code issue

I'm wondering how I can get the code below to work with my project as to combine the two so that it could work. What I have is an implemented Navigation Drawer in the app
1st Java Code Using Fragment:
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class PagesFragment extends Fragment {
public PagesFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_pages, container, false);
return rootView;
}
}
2nd Java Code Using Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AppointmentActivity extends Activity {
Button sendEmail;
EditText msg;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appointment_layout);
sendEmail = (Button) findViewById(R.id.sndBtn);
sendEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
msg = (EditText) findViewById(R.id.msgTxt);
String message = msg.getText().toString();
sendEmail(message);
}
});
}
protected void sendEmail(String message) {
String[] to=new String[]{"Shop#email.com"};
String subject=("Shop Appointment");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, message);
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Gmail"));
}
}
I've tried to combine the two myself but I don't have a lot of experience dealing with android to know how to make both of these codes work side by side and not give me a force close. Anything would be helpful!
To combine these codes, you need to declare your Activity like a FragmentActivity (it will be "host" your Fragment). See this answer: https://stackoverflow.com/a/10609839/2668136
And Google training Fragment
Also Google documentation FragmentActivity
Hope this help.

Categories