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".
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); // ???????
when i click on the "ChangeBudgetBtn" the next activity i.e. BudgetEdit does not open and the application closes without any prompt
package com.moneymgmt.moneymanagementsystem;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
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;
public class BudgetFragment extends Fragment {
Button ChangeBudgetBtn;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_budget, container,false);
ChangeBudgetBtn = (Button) rootView.findViewById(R.id.ChangeBudgetBtn);
ChangeBudgetBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(v.getContext(),BudgetEdit.class));
}
});
return rootView;
}
}
Replace this line
final View rootView = inflater.inflate(R.layout.fragment_budget, false);
by the following line
final View rootView = inflater.inflate(R.layout.fragment_budget, container, false);
because you forgot to add the container parameter
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;
}
}
I have a problem with createing a functionality in my app. I would like that the Button in one tab of my application update the TextViews which are located in the other tab of my application. Unfortunately my solutions does not work - throws NPE everytime I touch the Button1. My code:
Tab which should be updated:
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.TextView;
public class SummaryFragment extends Fragment {
TextView textViewOne;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View summary= inflater.inflate(R.layout.fragment_summary, container, false);
textViewOne = (TextView) summary.findViewById(R.id.books);
textViewOne.setText("You have read " + Stats.book + " books");
return summary;
}
public void updateStats() {
textViewOne.setText("You have read " + Stats.book + " books");
}
}
Tab on which the button is located:
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;
public class ReadingStarted extends Fragment {
static FirstPageFragmentListener firstPageListener;
public SummaryFragment summaryFragment;
public ReadingStarted() {
}
public ReadingStarted(FirstPageFragmentListener listener) {
firstPageListener = listener;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View started = inflater.inflate(R.layout.fragment_started, container, false);
Button button1 = (Button) started.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Stats.book=Stats.book+1;
summaryFragment.updateStats();
}
});
return started;
}
}
Problems in your code:
SummaryFragment doesnot have a empty constructor every fragment requires it
SummaryFragment summaryFragment should be SummaryFragment summaryFragment = new SummaryFragmet();//no initialization done so npe in summaryFragment.updateStats();
Communication between fragments should be done through activity.
See the developer docs about Fragments.