I have an fragment where I try to Instantiate a new newRequestQueue with the Volley API.
I try to instantiate it like this:
RequestQueue queue = Volley.newRequestQueue(this);
However, when I try to create the request, I get the following error:
newRequestQueue In Volley cannot be applied to annonymous android.view.View.OnClickListener
Here is my Fragment class:
public class LoginFragment extends Fragment {
private FacebookLogin fLogin;
private Profile profile;
private CallbackManager mCallbackManager; //Used in activity result below. Gets a value when u hit the button
private static final String TAG = "Event";
private static String url_create_user = "127.0.0.1/create_row.php";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fLogin = new FacebookLogin(getActivity().getApplicationContext());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.login, container, false);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
final LoginButton loginButton = (LoginButton) getView().findViewById(R.id.login_button);
final TextView infoText = (TextView) getView().findViewById(R.id.text_details);
final ImageView profilP = (ImageView) getView().findViewById(R.id.profilePicture);
loginButton.setFragment(this);
loginButton.setReadPermissions("user_friends");
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i(TAG, " Button clickde");
fLogin.setCallback(loginButton); //Let's register a callback
RequestQueue queue = Volley.newRequestQueue(this);
fLogin.setFacebookListener(new FacebookLogin.OnFacebookListener() {
#Override
public void onFacebookLoggedIn(JSONObject parameters) {
Log.i(TAG, "INNNNNNNNNNNE");
Log.i(TAG, parameters.toString());
}
});
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager = fLogin.getCallbackManager(); //Get the callbackmanager
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
the parameter of newRequestQueue is a Context object. In your case, this, refers to the View.OnClickListener anonymous inner class, where you are calling newRequestQueue. Change
Volley.newRequestQueue(this);
with
Volley.newRequestQueue(getActivity().getApplicationContext());
is, of course, getActivity() because you are subclassing Fragment
RequestQueue requestQueue = Volley.newRequestQueue(SignupActivity.this);
In this case, SignupActivity is the activity you have typed your code in.
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
In my app, i have an activity that have recyclerview inside a fragment and the recycleview retrieve data from cloud store Firebase. I want to open new activity that will retrieve the data according to the link(that display on the recycler view) user has clicked.
How do i get the data and display it in new activity?
From ForumTitle.java > Show ReviewFragment.java > User click a value > Show ForumInterface.java
ForumTitle.java
public class ForumTitle extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
ImageButton IVReview,IVTechnical,IVHardware;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forum_title);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Fragment selectedFragment = null;
if (v == findViewById(R.id.iBReview)){
selectedFragment = new ReviewFragment();
}
else if (v == findViewById(R.id.iBTech)){
selectedFragment = new TechnicalSupportFragment();
}
else if (v == findViewById(R.id.iBHardware)){
selectedFragment = new HardwareFragment();
}
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_container,selectedFragment);
transaction.commit();
}
};
IVReview = (ImageButton)findViewById(R.id.iBReview);
IVTechnical = (ImageButton)findViewById(R.id.iBTech);
IVHardware = (ImageButton)findViewById(R.id.iBHardware);
IVReview.setOnClickListener(listener);
IVTechnical.setOnClickListener(listener);
IVHardware.setOnClickListener(listener);
}
}
ReviewFragment.java
public class ReviewFragment extends Fragment {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference userRef = db.collection("Review");
private ForumAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_review,container,false);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
Query query = userRef.orderBy("DatePosted",Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Forum> options = new
FirestoreRecyclerOptions.Builder<Forum>()
.setQuery(query,Forum.class)
.build();
adapter = new ForumAdapter(options);
RecyclerView recyclerView = (RecyclerView)view.findViewById(R.id.rvReview);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new ForumAdapter.OnItemClickListener() {
#Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
Forum forum = documentSnapshot.toObject(Forum.class);
String title = forum.getTitle();
String id = documentSnapshot.getId();
Intent intent = new Intent(getActivity(), ForumInterface.class);
Bundle extras = intent.getExtras();
extras.putString("FORUM_TYPE","Review");
extras.putString("FORUM_ID",id);
extras.putString("TITLE",title);
startActivity(intent);
}
});
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
ForumInterface.java
public class ForumInterface extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String forum_title = extras.getString("TITLE");
String forum_type = extras.getString("FORUM_TYPE");
String forum_id = extras.getString("FORUM_ID");
private CollectionReference userRef = db.collection(forum_type).document(forum_id).collection(forum_title);
private ForumAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forum_interface);
TextView test = (TextView)findViewById(R.id.tvForumTitle);
test.setText(forum_title);
}
}
ForumAdapter.java
public class ForumAdapter extends FirestoreRecyclerAdapter<Forum,ForumAdapter.ForumHolder> {
private OnItemClickListener listener;
public ForumAdapter(FirestoreRecyclerOptions<Forum> options) {
super(options);
}
#Override
public void onBindViewHolder(ForumHolder forumHolder, int i, Forum forum) {
forumHolder.textViewTitle.setText(forum.getTitle());
forumHolder.textViewDescription.setText(forum.getDescription());
forumHolder.timeStamp.setText(forum.getDatePosted().toString());
}
#NonNull
#Override
public ForumHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
return new ForumHolder(v);
}
class ForumHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
TextView textViewDescription;
TextView timeStamp;
public ForumHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.title);
textViewDescription = itemView.findViewById(R.id.description);
timeStamp = itemView.findViewById(R.id.timestamp);
textViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
// NO_POSITION to prevent app crash when click -1 index
if(position != RecyclerView.NO_POSITION && listener !=null ){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
#Override
public int getItemCount() {
return super.getItemCount();
}
}
With the code above i run it, i got an error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Bundle.putString(java.lang.String, java.lang.String)' on a null object reference
at my.edu.fsktm.um.finalproject.Fragment.ReviewFragment$1.onItemClick(ReviewFragment.java:61)
at my.edu.fsktm.um.finalproject.ForumTitle.ForumAdapter$ForumHolder$1.onClick(ForumAdapter.java:56)
The picture of my app
When I clicked the title "GTX 1660 Ti Gaming X"
You try to set content on a Bundle which is null in your ReviewFragment's onClick. Use new Bundle() instead of intent.getExtras(); to instantiate Bundle
Bundle extras = new Bundle();
extras.putString("FORUM_TYPE","Review");
extras.putString("FORUM_ID",id);
extras.putString("TITLE",title);
//You have to set the bundle to intent
intent.putExtras(extras);
startActivity(intent);
Beside this move below code inside onCreate of ForumInterface Activity
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String forum_title = extras.getString("TITLE");
String forum_type = extras.getString("FORUM_TYPE");
String forum_id = extras.getString("FORUM_ID");
And here is the complete Activity.
public class ForumInterface extends AppCompatActivity {
private FirebaseFirestore db;
private CollectionReference userRef;
private ForumAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forum_interface);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String forum_title = extras.getString("TITLE");
String forum_type = extras.getString("FORUM_TYPE");
String forum_id = extras.getString("FORUM_ID");
db = FirebaseFirestore.getInstance();
userRef = db.collection(forum_type).document(forum_id).collection(forum_title);
TextView test = (TextView)findViewById(R.id.tvForumTitle);
test.setText(forum_title);
}
}
my promblem is : i cant use animition for intent becuse this calss extend recyclerView adapter i know overridePendingTransition();but its dont work for tis class
It does not know => overridePendingTransition(R.layout.one,R.layout.two);
public class ListAdapterw extends RecyclerView.Adapter {
List<cardViewhomeinfo> sliders;
ImageLoader imageLoader=ImageLoader.getInstance();
public ListAdapterw(List<cardViewhomeinfo> sliders) {
this.sliders = sliders;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view=inflater.inflate(R.layout.recycleview_home,parent,false);
return new Holder(view);
}
#Override
public void onBindViewHolder(Holder holder, int position) {
holder.title.setText(sliders.get(position).getTitle());
holder.id=sliders.get(position).getId();
imageLoader.displayImage("http://wwwww"+sliders.get(position).getPicture(),holder.pic);
}
#Override
public int getItemCount() {
return sliders.size();
}
public class Holder extends RecyclerView.ViewHolder{
public TextView title;
public ImageView pic;
public String id="";
CardView cardView;
public Holder(final View itemView) {
super(itemView);
title=(TextView)itemView.findViewById(R.id.TitleCardView_home);
pic=(ImageView)itemView.findViewById(R.id.imageCardView_home);
cardView= (CardView) itemView.findViewById(R.id.cardView);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(itemView.getContext(),subCat.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("id",id);
overridePendingTransition(R.layout.one,R.layout.two);
itemView.getContext().startActivity(intent);
}
});
}
}
}
and my activtity
public class Home extends Fragment {
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_home, container, false);
JSON();
return view;
}
private void JSON() {
final RecyclerView recyclerView1 = (RecyclerView) view.findViewById(R.id.recycle_home);
StringRequest request=new StringRequest(Request.Method.GET, "", new Response.Listener<String>() {
#Override
public void onResponse(String s) {
try {
List<cardViewhomeinfo> sliders = new ArrayList<>();
JSONArray jsonArray=new JSONArray(s);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
cardViewhomeinfo slider = new cardViewhomeinfo();
slider.setTitle(jsonObject.getString("title"));
slider.setPicture(jsonObject.getString("pic"));
slider.setId(jsonObject.getString("id"));
sliders.add(slider);
}
ListAdapterw adapter = new ListAdapterw(sliders);
recyclerView1.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
recyclerView1.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
dialog();
}
});
Volley.newRequestQueue(getActivity().getApplicationContext()).add(request);
}
private void dialog() {
Toast.makeText(getActivity().getApplicationContext(),"No INTENT",Toast.LENGTH_LONG).show();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Home");
}
}
and subActivty
public class subCat extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_cat);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
final String idCat = intent.getStringExtra("id");
Toast.makeText(getApplicationContext(),idCat,Toast.LENGTH_LONG).show();}
Put your overridePendingTransition(R.layout.one,R.layout.two); code into the onCreate of the activity that you're going to when you click on cardView.
It should be something like this in your onCreate method of the activity:
public class subCat extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sub_cat);
overridePendingTransition(R.layout.one,R.layout.two); //Put it here.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
final String idCat = intent.getStringExtra("id");
Toast.makeText(getApplicationContext(),idCat,Toast.LENGTH_LONG).show();
}
Remove overridePendingTransition(R.layout.one,R.layout.two); this from the adapter class of your recycler view.
On the other hand,
overridePendingTransition(R.layout.one,R.layout.two);
it should be called
after finish();
or
after startActivity();
My issue is very simple, I want users to login using Facebook. After a successful login, grab some details and pass it on another activity. I have my new class in manifeset. I see the log for Log.v("facebook - profile", profile.getFirstName()); I'm quite baffled about what's going on. I have no errors either. I'm passing my activity in the onSuccess.
public class MainActivityFragment extends Fragment {
TextView textview;
private AccessTokenTracker tokenTracker;
private ProfileTracker profileTracker;
private CallbackManager mCallbackManager;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
if(Profile.getCurrentProfile() == null) {
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
Log.v("facebook - profile", profile2.getFirstName());
profileTracker.stopTracking();
}
};
profileTracker.startTracking();
}
else {
Profile profile = Profile.getCurrentProfile();
Log.v("facebook - profile", profile.getFirstName());
//get info to pass to next activity
String[] profileDetailsArray = new String [5];
profileDetailsArray[0] = profile.getFirstName();
profileDetailsArray[1] = profile.getLastName();
profileDetailsArray[2] = profile.getId();
Bundle arrayBundle = new Bundle();
arrayBundle.putStringArray("profileDetails",profileDetailsArray);
Intent intent = new Intent(getActivity(),LandingPage.class);
intent.putExtras(arrayBundle);
startActivity(intent);
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
};
public MainActivityFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
AccessTokenTracker tokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "public_profile"));
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager,mCallback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onResume(){
super.onResume();
Profile profile = Profile.getCurrentProfile();
}
#Override
public void onStop(){
super.onStop();
}
}
How and Where do I set view contents to be displayed for persons who has successfully been authenticated using Facebook SDK?
After the persons has logged In, I want to display a User profile view.
I have tried to Instansiate a new fragment like the code below, but that don't work. The text "Fish" Is not displayed.
public class LoginFragment extends Fragment {
private CallbackManager mCallbackManager;
private static final String TAG = "Facebook";
private LoginButton login;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i(TAG, " logged in...");
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile(); //Access the profile who Is the person login in
if(profile != null) {
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if(fragment == null) {
fragment = new LoggedInFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
}
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
Log.i(TAG, " Error");
}
};
public LoginFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.login, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
//Ask the use for permission to access friends
loginButton.setReadPermissions("user_friends");
//Because we work with fragments
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager, mCallback);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
}
Here IS fragement that the user should see when he/she Is logged In:
package com.waddapp.bryan.waddapp;
public class LoggedInFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.logged_in, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
TextView mTextDetails = (TextView)view.findViewById(R.id.text_details);
mTextDetails.setText("FISH");
}
public LoggedInFragment() {
}
}
Anyone who can explain how acomplish this?
In my App I have a MainActivity, which makes use of different fragments. One of these fragments is kind of a detail fragment, where I want to capture some serials by scanning a QR code. Currently, I am trying to use the IntentIntegrator. I am able to scan the code successfully, but after that, my App is not returning correctly. It just displays my MainActivity, but there is no Toast.
I also tried to put a onActivityResult() in the fragment and a super.onActivityResult()in the activities onActivityResult() but it is always the same behaviour. It just jumps into the MainActivity and nothing futher happens.
Can somebody exlain to me, where I made the mistakes? I want to scan the barcode and get the results in my fragment class.
Here is my MainActivity Code:
public class MainActivity extends Activity {
private Button startenButton, ansehenButton, abmeldenButton;
private FrameLayout fragmentFrame;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentFrame = (FrameLayout) findViewById(R.id.frameLayout);
startenButton = (Button) findViewById(R.id.buttonPruefungStart);
startenButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragmentFrame.getChildCount() < 1) {
transaction.add(R.id.frameLayout, new PruefungStartenFragment());
} else {
transaction.replace(R.id.frameLayout, new PruefungStartenFragment());
}
transaction.commit();
}
});
}
public void scan() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {
Toast.makeText(this, scanResult.getContents(), Toast.LENGTH_LONG).show();
}
}
}
And here is my PruefungStartenFragment Code:
public class PruefungStartenFragment extends Fragment {
private TextView datum, ort, pruefer, bedienerdisplay, scanner, fingerprintscanner, quittungsdrucker, barcodeleser,
webcam, kundendisplay, terminal;
private Button scanButton, startButton;
public static PruefungStartenFragment newInstance(String code) {
Bundle args = new Bundle();
args.putSerializable("code", code);
PruefungStartenFragment fragment = new PruefungStartenFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.pruefung_starten_fragment, container, false);
scanButton = (Button) v.findViewById(R.id.buttonScan);
scanButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).scan();
}
});
return v;
}
}
I figured out my problem... In my manifest I had a android:noHistory="true" in my MainActivity. This prevents the scanning activity in the barcode scanner to pass the result to my MainActivity, because there is no activity on the stack, where it could pass results to.