I have a problem with fragment:
package com.example.navbar;
import android.content.Intent;
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.annotation.Nullable;
import androidx.fragment.app.Fragment;
import org.jetbrains.annotations.NotNull;
public class MenuFragment extends Fragment {
Button buttonvax;
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
getActivity().setTitle("Menu");
View view = inflater.inflate(R.layout.menu_fragment,null);
return view;
buttonvax = getView().findViewById(R.id.buttonVaccini);
buttonvax.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), userlist.class);
startActivity(intent);
}
});
}
it give me an error :
Intent intent = new Intent(MenuFragment.this, userlist.class);
^
constructor Intent.Intent(String,Uri) is not applicable
(argument mismatch; MenuFragment cannot be converted to String)
constructor Intent.Intent(Context,Class<?>) is not applicable
(argument mismatch; MenuFragment cannot be converted to Context)
I want to create button that open a new fragment call userlist
why this code is wrong?
buttonvax = getView().findViewById(R.id.buttonVaccini);
buttonvax.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), userlist.class);
startActivity(intent);
}
});
i usually ude this code on activity but i don't know what to do on fragment
Related
this is my first android project.
I am making a login and register page for a game i am making, and i'm trying to figure the login/register stuff. my project so far consists of MainActivity.java, LoginFragment and a RegisterFragment.
my question is after declaring the username, password, loginbtn variables how should my Intent intent declaration look like?
note aside, my mainActivity consists of a viewPager which just lets me slide through the login and register pages. but i will supply the code below
mainActivity.java
package com.example.my_app_2;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.app.Dialog;
import android.content.Intent;
import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button btnSignIn, btnSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create instance of SQLite Database
ViewPager viewPager = findViewById(R.id.viewPager);
AuthenticationPagerAdapter pagerAdapter = new AuthenticationPagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragmet(new LoginFragment());
pagerAdapter.addFragmet(new RegisterFragment());
viewPager.setAdapter(pagerAdapter);
}
static class AuthenticationPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragmentList = new ArrayList<>();
public AuthenticationPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
#Override
public int getCount() {
return fragmentList.size();
}
void addFragmet(Fragment fragment) {
fragmentList.add(fragment);
}
}
}
LoginFragment:
package com.example.my_app_2;
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;
/**
* A simple {#link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
EditText username, password;
Button loginbtn;
public LoginFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, container,false);
username = v.findViewById(R.id.username);
password = v.findViewById(R.id.password);
loginbtn = v.findViewById(R.id.loginbtn);
loginbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String usernameValue = username.getText().toString();
String passwordValue = password.getText().toString();
// i assume the Intent intent goes here, but i have no idea what arugments/parameters pass in the new Intent() part of it
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false);
}
}
and same goes with the registerFragment
package com.example.my_app_2;
import android.content.Intent;
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;
/**
* A simple {#link Fragment} subclass.
*/
public class RegisterFragment extends Fragment {
EditText username, password, email;
Button signupbtn;
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_register, container,false);
username = v.findViewById(R.id.username);
password = v.findViewById(R.id.password);
email = v.findViewById(R.id.email);
signupbtn = v.findViewById(R.id.signupbtn);
signupbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String usernameValue = username.getText().toString();
String passwordValue = password.getText().toString();
String emailValue = email.getText().toString();
Intent intent = new Intent();
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
}
what i have tried to do...
what i am trying to do is just take the username and password and store them in sharedPreferences.
thanks in advance, i apoligize for the silliness of the question but any help would be appreciated!
i have tried Intent intent = new Intent(this, RegisterFragment); // ??????
honestly im kinda clueless lol xd
(RegisterFragment.this); // ???????
I have two file with name
FirstFragment.java
SecondFragment.java
Code of the FirstFragment is
package com.example.application
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
return view;
}
// The method that i want to call from SecondFragment
public void methodFirst() {
Toast.makeText(getContext(), "Method First Clicked", Toast.LENGTH_SHORT).show();
}
}
Now I want to call the methodFirst function in SecondFragment. I have tried the following code on SecondFragment.
Code of the SecondFragment is
package com.example.application
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment {
Private AppCompatButton btnOneSecondFragment;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container, false);
btnOneSecondFragment = (AppCompatButton) view.findViewById(R.id.btn_one_second_fragment);
FirstFragment firstFrag = new FirstFragment();
btnOneSecondFragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
firstFrag.methodFirst();
}
});
return view;
}
}
But the following error is displaying
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String android.content.Context.getPackageName()' on a null
object reference
I'm using zxing library to read qr codes. It works well in MainActivity. Then I want to add that code in a fragment. But intent integrator is not working for this fragment.
This is the code in QrFragment;
package com.myapp.agpj.qr;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import com.google.zxing.integration.android.IntentIntegrator;
public class QrFragment extends Fragment {
public QrFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_qr, container, false);
Button scan = (Button) v.findViewById(R.id.bQr);
final Fragment activity = this;
scan.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view){
IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setPrompt("Scan QR codes in the school premises");
integrator.setCameraId(0);
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(false);
integrator.initiateScan();
}
});
return v;
}
}
This outputs an error "IntentIntegator can't applied to android.support.v4.fragment".
I'm writing a simple program in which a two fragments and one activity is used. Both Fragments are displayed (one at a time) within the activity's Frame Layout. The first fragment is a listview that lists items from which the user can select, then the main activity should swap the first fragment with a detail fragment according to the item position determined by a listener within the first fragment. Trouble is, my program won't actually commence the swap. Here's the code for the activity:
package com.example.user.monkeys;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
public class ListActivity extends FragmentActivity implements
monkeyListFragment.OnMonkeySelectedListener {
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i("Activity", "onCreate Pre-Fragment 1");
super.onCreate(savedInstanceState);
setContentView(R.layout.monkey_list_frame);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
monkeyListFragment monkeyList = new monkeyListFragment();
monkeyList.setArguments(getIntent().getExtras());
// Add the fragment to the container
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, monkeyList);
Log.i("Activity", "made it end onCreate");
}
}
#Override
public void onMonkeyItemSelected(int position) {
Log.i("From Activity", "onMonkeyItemSelected");
monkeyDetailsFragment newDetailFrag = new monkeyDetailsFragment();
Bundle args = new Bundle();
args.putInt("itemPosition", position);
newDetailFrag.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, newDetailFrag);
transaction.addToBackStack(null);
transaction.commit();
}
}
And the code for the listview fragment:
package com.example.user.monkeys;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class monkeyListFragment extends Fragment{
OnMonkeySelectedListener monkeyCallBack;
private ListView monkeyLV;
private String[] monkeyStrings;
public interface OnMonkeySelectedListener {
public void onMonkeyItemSelected(int position);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i("Fragment 1", "Made it to onCreateView");
View view = inflater.inflate(R.layout.list_fragment, container, false);
monkeyLV = (ListView) view.findViewById(R.id.monkeyListView);
monkeyStrings = getResources().getStringArray(R.array.monkey_data_list);
ArrayAdapter<String> objAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, monkeyStrings);
monkeyLV.setAdapter(objAdapter);
AdapterView.OnItemClickListener monkeyListen = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ListFragment-ClckLstnr", "Made it");
monkeyCallBack.onMonkeyItemSelected(position);
}
};
Log.i("Fragment 1", "Made it past OnItemClick Listener");
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
monkeyCallBack = (OnMonkeySelectedListener) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString());
}
}
}
and finally the code for the detail fragment (this isn't fully fleshed out but it should still swap I believe).
package com.example.user.monkeys;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class monkeyDetailsFragment extends Fragment {
ImageView monkeyPicture;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
Log.i("From Detail Fragment", "Got here");
View view = inflater.inflate(R.layout.monkey_detail_fragment, container,
false);
return view;
}
}
I found your missing line of code for the listview fragment:
monkeyLV.setOnItemClickListener(monkeyListen)
I'm struggling with these errors for a few days already, I tried googling it, but unfortunately I can't find any fixes for this.
I'm still a beginner in Java for Android.
So I got a NavigationDrawer with a Fragment, in that fragment i want to put a button which is clickable, but somehow it just doesn't recognize the button and some things while I've got it in my layout.
This is my code:
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Souf on 21-9-2015.
*/
public class First_Fragment extends Fragment {
View MyView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
return MyView;
View rootView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) rootView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onClick(v);//THIS IS THE METHOD YOU WROTE ON THE ATTACHED CODE!!
}
});
}
}
Screenshot of my project:
First_Fragment Class
http://i.stack.imgur.com/5W2uG.png
Layout of first_fragment:
http://i.imgur.com/ndVmcX2.png
I'm getting the following errors:
Cannot resolve symbol 'OnClickListener' (new OnClickListener)
Method does not override method from its superclass (#Override)
Parameter 'v' is never used. (View v)
Method OnClick is never used (public void onClick)
Cannot resolve symbol 'OnClickListener' (new OnClickListener)
Add this in your imports : import android.view.View.OnClickListener;
You just have to choose one of your views, since you have 2 I don't know why rootView and MyView then your onClickListener() should be :
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//your stuff here
}
});
Parameter 'v' is never used. (View v)
Just remove this onClick(v)
Your coude should work if you do this :
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff there
}
});
return MyView;
}
All fo your code
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;
/**
* Created by Souf on 21-9-2015.
*/
public class First_Fragment extends Fragment {
View MyView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
MyView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) MyView.findViewById(R.id.button2);
c.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Stuff there
}
});
return MyView;
}
}
You must return your rootView at the end of your code, because return means "end that method", so it won't continue the code below it in the same method.
Replace your code with this one:
package nl.c99.c99nlapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class First_Fragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_layout, container, false);
Button c = (Button) rootView.findViewById(R.id.button2);
c.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Write your code here.
}
});
return rootView;
}
}