Android Fragment Code issue - java

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.

Related

fragment binding (symbol "id" cannot be resolved)

I am trying to use the binding method in android studio to connect two fragments using onCreate and onViewCreated methods. so far i am getting id not resolved error. I have already connected the fragments on the xml graph. Bellow is the code of the settings java file.
package com.mqtt.workactiv.ui.settings;
import static android.os.Build.VERSION_CODES.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.mqtt.workactiv.databinding.FragmentSettingsBinding;
public class SettingsFragment extends Fragment {
private FragmentSettingsBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
SettingsViewModel settingsViewModel =
new ViewModelProvider(this).get(SettingsViewModel.class);
binding = FragmentSettingsBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
#Override
public void onViewCreated (#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController = Navigation.findNavController(view);
Button gateConnButton;
gateConnButton = view.findViewById(R.id.gatewayConnButton);
gateConnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_navigation_setting_to_gateway4);
}
});
}
}
Make sure to add this in build.gradle(app)
buildFeatures {
viewBinding = true
}
Then give sync project with gradle files.
If this didnt work, try rebuild, invalidate cache/ restart.
Also view.findviewbyid not required if view binding is enabled.
You can access button directly by something like this,
binding.gateConnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_navigation_setting_to_gateway4);
}
});
You can try code like this.
NavDirections navDirections = SettingsFragmentDirections.actionNavigationSettingToGateway4();
findNavController().navigate(navDirections, null);
I don't see your navigation xml file, so SettingsFragmentDirections.actionNavigationSettingToGateway4() can have another name. Fix those names yourself.
P.S. the versions of libraries which I'm using androidx.navigation:navigation-fragment-ktx:2.4.0, "androidx.navigation:navigation-ui-ktx:2.4.0"

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

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.

Application crashes on clicking a link in TextView

I am working on an application called Android Glossary. In my second activity I have created a TextView which has a link embedded in it. Everything seems fine; the link is highlighted in blue. The problem is that when I click on the link, my application crashes down. I don't know what is wrong. Please help.
Code in my second activity :
package com.mavenmaverick.androidglossary;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class CActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
Intent browser= new Intent(Intent.ACTION_VIEW, adress);
startActivity(browser);
}
});
}
see your code
you have enter url like
Uri adress= Uri.parse("http;//www.cyanogenmod.com");
replace with this
Uri adress= Uri.parse("http://www.cyanogenmod.com");
problm is ; in Http; replace with http:
you should use setData()
String url = "http://www.cynogenmod.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
TextView link = (TextView) findViewById(R.id.link);
link.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browser = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.cyanogenmod.com"));
startActivity(browser);
}
});

My app will only output "false", not the code I want

I used samples from similar code to make this, unfortunately I'm not to sure what I did wrong.
The purpose of this app is to output text entered in a field to a TextView, where I changed the color, when a button is pressed.
package edu.wmich.project3
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class Main extends Activity {
String txtResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button text =(Button) findViewById(R.id.btnColor0);
final TextView result = ((TextView) findViewById(R.id.txtResult));
text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
txtResult= getText(R.id.txtField0).toString();
result.setText(txtResult);
}
});
}
}
txtResult = (findViewById(R.id.txtField0)).toString();
will solve...
the problem is that you're using the method getText() from the Activity which has nothing to do with the TextField you're dealing with.
...what is the type of view of R.id.txtField0? I guess with this you can take it from here.

tabhost inside actionbar using fragment

I have used actionbar and it is working fine. I have four fragment in the actionbar. I want to implement tabhost in one of the fragment. I have used the following code.
package com.main.udebate;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
public class Info extends Fragment {
public Info() {
}
public static final String ARG_SECTION_NUMBER = "section_number";
private TabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent i = new Intent(Info.this, about.class);
View view = inflater.inflate(R.layout.info, container, false);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
tab.setIndicator("my tab content");
tab.setContent(i);
mTabHost.addTab(tab);
mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
return view;
}
}
I am getting error on Intent i = new Intent(Info.this, about.class); line as The constructor Intent(Info, Class) is undefined.
About.java
package com.main.udebate;
import android.app.Activity;
import android.os.Bundle;
public class about extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
Can someone pls help me to set tabhost inside fragment.
Thanks,
try to use this
Intent i = new Intent(getActivity(), about.class);
instead of this line
Intent i = new Intent(Info.this, about.class);
As Info.this does not mean any context thats why you get "The constructor Intent(Info, Class) is undefined." this error.
In fragment where you use this reference you should instead use getActivity() reference

Categories