i am working with the facebook sdk for android and i was following a tutorial. When i ran my code it says java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ftester/com.example.ftester.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.ftester.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.ftester-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.ftester-2, /vendor/lib, /system/lib]]
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION +1;
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private Session.StatusCallback callback =
new Session.StatusCallback() {
#Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SPLASH, false);
}
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
showFragment(SELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(SPLASH, false);
}
}
}
#Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ftester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ftester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
There's a possibility that your MainActivity.java file is not being compiled and not available in the apk file. Check and try this solution https://stackoverflow.com/a/18533212/3025732
Related
so I'm trying to refactor my app that uses the Facebook API to use fragments instead of activities. Before, after logging in with Facebook, I would get taken to a new activity - now, I'm trying to use fragments instead. However, ever since I started using fragments, it's been failing during requests, giving me an error "Session provided to a Request in un-opened state"
I added all the UIHelper lifecycle methods to my fragment, but I'm still getting the error. I basically copied the exact code that the example fragment uses, and I'm still getting this issue. Can anybody help me out? Here's my fragment, and the activity that sets it up (most of it is stock code taken directly from Facebook's example)
Fragment:
public class ConvoFragment extends Fragment {
String name;
ListView convos;
SwipeRefreshLayout rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
rootView = (SwipeRefreshLayout)inflater.inflate(R.layout.convos_fragment,
container, false);
convos = (ListView) rootView.findViewById(R.id.conversation_list);
name = getActivity().getSharedPreferences("global", 0).getString("myName", "");
Session session = Session.getActiveSession();
RequestAsyncTask req = new Request(session, "/me/inbox/", null, HttpMethod.GET, new Request.Callback()
{
public void onCompleted(Response response)
{
final Response resp = response;
try
{
if (response != null)
{
GraphObject asdf2 = response.getGraphObject();
Log.i("graphobject", response.toString());
//This logs the Session Provided to Request in unopened state error. Everything below will fail
JSONObject obj = asdf2.getInnerJSONObject();
JSONArray threads= obj.getJSONArray("data");
final ConvoAdapter adapter = new ConvoAdapter(getActivity(), convertArray(threads), name);
convos.setAdapter(adapter);
setupRefreshListener();
setupItemListener(adapter);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}).executeAsync();
return rootView;
}
So it fails right as it makes the request, because the request essentially returns null. Here's my activity that sets up the fragments:
public class LoginActivity extends AppCompatActivity {
private static final String USER_SKIPPED_LOGIN_KEY = "user_skipped_login";
private static final int SPLASH = 0;
public static final int FILE = 1;
public static final int CONVOS = 2;
public static final int SETTINGS = 3;
public static final int ABOUT = 4;
private static final int FRAGMENT_COUNT = ABOUT +1;
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private boolean isResumed = false;
private boolean userSkippedLogin = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
userSkippedLogin = savedInstanceState.getBoolean(USER_SKIPPED_LOGIN_KEY);
}
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
getSupportActionBar().setLogo(R.drawable.chatstatssmall);
FragmentManager fm = getSupportFragmentManager();
MainFragment splashFragment = (MainFragment) fm.findFragmentById(R.id.splashFragment);
fragments[SPLASH] = splashFragment;
fragments[FILE] = fm.findFragmentById(R.id.fileFragment);
fragments[CONVOS] = fm.findFragmentById(R.id.convoFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
fragments[ABOUT] = fm.findFragmentById(R.id.aboutFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
android.support.v7.app.ActionBar act = getSupportActionBar();
act.setTitle("Welcome to ChatStats!");
act.setHomeAsUpIndicator(R.drawable.chatstatssmall);
act.setHomeButtonEnabled(true);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_settings:
showFragment(SETTINGS, false);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public final static String extra_msg = "chatstats.passArray";
#Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
// Call the 'activateApp' method to log an app event for use in analytics and advertising reporting. Do so in
// the onResume methods of the primary Activities that an app may be launched into.
AppEventsLogger.activateApp(this);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
// Call the 'deactivateApp' method to log an app event for use in analytics and advertising
// reporting. Do so in the onPause methods of the primary Activities that an app may be launched into.
AppEventsLogger.deactivateApp(this);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
outState.putBoolean(USER_SKIPPED_LOGIN_KEY, userSkippedLogin);
}
#Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open, try to show the selection fragment
userSkippedLogin = false;
showFragment(FILE, false);
} else if (userSkippedLogin) {
showFragment(FILE, false);
} else {
// otherwise present the splash screen and ask the user to login, unless the user explicitly skipped.
showFragment(SPLASH, false);
}
}
public void startMainActivity(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
int backStackSize = manager.getBackStackEntryCount();
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
// check for the OPENED state instead of session.isOpened() since for the
// OPENED_TOKEN_UPDATED state, the selection fragment should already be showing.
if (state.equals(SessionState.OPENED)) {
showFragment(FILE, false);
} else if (state.isClosed()) {
showFragment(SPLASH, false);
}
}
}
public void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
}
This uiHelper stuff is really confusing me. I'd appreciate if anybody could let me know what I might be doing wrong. I thought I followed the Facebook documentation (available here https://developers.facebook.com/docs/reference/android/3.23.1/class/UiLifecycleHelper/ ) exactly, but I'm still getting this error.
I am new to android and currently I want to develop an application that need to request network using volley. However, it returns error below:
Caused by: java.lang.IllegalStateException: RequestQueue not initialized
at com.myapp.zeptomobile.myapp.app.StaggeredDemoApplication.getRequestQueue(StaggeredDemoApplication.java:35)
at com.myapp.zeptomobile.myapp.FlickrActivity.onCreate(FlickrActivity.java:75)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2164)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2249)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1212)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5052)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
01-18 14:07:15.655 24065-24065/com.myapp.zeptomobile.myapp I/Process: Sending signal. PID: 24065 SIG: 9
Below is the Main Program
enter public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, FragmentRecent.OnFragmentInteractionListener,
FragmentAseanGirl.OnFragmentInteractionListener,FragmentFlickrStart.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null) {
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = FragmentRecent.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
Class fragmentClass = null;
if (id == R.id.nav_camera) {
fragmentClass = FragmentRecent.class;
} else if (id == R.id.nav_gallery) {
fragmentClass = FragmentAseanGirl.class;
} else if (id == R.id.nav_slideshow) {
fragmentClass = FragmentRecent.class;
} else if (id == R.id.nav_manage) {
fragmentClass = FragmentFlickrStart.class;
} else if (id == R.id.nav_share) {
fragmentClass = FragmentRecent.class;
} else if (id == R.id.nav_send) {
fragmentClass = FragmentAseanGirl.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
Below is the Fragment program
public class FragmentFlickrStart extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public FragmentFlickrStart() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment FragmentTwo.
*/
// TODO: Rename and change types and number of parameters
public static FragmentFlickrStart newInstance(String param1, String param2) {
FragmentFlickrStart fragment = new FragmentFlickrStart();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
Intent intent = new Intent(getActivity(),FlickrActivity.class);
startActivity(intent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_flickrstart, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Below is the Activity program
public class FlickrActivity extends Activity{
private StaggeredGridView mStaggeredView;
private RequestQueue mVolleyQueue;
private ProgressDialog mProgress;
private int currPage=1;
GsonRequest<FlickrResponsePhotos> gsonObjRequest;
private RelativeLayout mListFooter;
private boolean isLoading = false;
private final String TAG_REQUEST = "MY_TAG";
private StaggeredGridView.OnScrollListener scrollListener = new StaggeredGridView.OnScrollListener() {
public void onTop() {
}
public void onScroll() {
}
public void onBottom() {
loadMoreData();
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flickr);
//actionBarSetup();
// Initialise Volley Request Queue. added to fix TBS
if(mVolleyQueue==null){
mVolleyQueue= Volley.newRequestQueue(getApplicationContext());
}
mVolleyQueue = StaggeredDemoApplication.getRequestQueue();
mListFooter = (RelativeLayout) findViewById(R.id.footer);
mStaggeredView = (StaggeredGridView) findViewById(R.id.staggeredview);
// Be sure before calling initialize that you haven't initialised from XML
//mStaggeredView.initialize(2, StaggeredGridView.Mode.FIXED);
mStaggeredView.setOnScrollListener(scrollListener);
showProgress();
flickerGetImagesRequest();
}
public void onStop() {
super.onStop();
if(mProgress != null)
mProgress.dismiss();
}
private void loadMoreData() {
if ( isLoading )
return;
mListFooter.setVisibility(View.VISIBLE);
isLoading = true;
flickerGetImagesRequest();
}
private void flickerGetImagesRequest() {
String url = "https://api.flickr.com/services/rest";
Uri.Builder builder = Uri.parse(url).buildUpon();
builder.appendQueryParameter("api_key", "5e045abd4baba4bbcd866e1864ca9d7b");
//builder.appendQueryParameter("method", "flickr.interestingness.getList"); //TBS
builder.appendQueryParameter("method", "flickr.photos.search");
builder.appendQueryParameter("tags","bikinigirl,lingerine");
//builder.appendQueryParameter("sort","relevance");
builder.appendQueryParameter("format", "json");
builder.appendQueryParameter("nojsoncallback", "1");
builder.appendQueryParameter("per_page", "10");
builder.appendQueryParameter("page", Integer.toString(currPage));
gsonObjRequest = new GsonRequest<FlickrResponsePhotos>(Request.Method.GET, builder.toString(),
FlickrResponsePhotos.class, null, new Response.Listener<FlickrResponsePhotos>() {
#Override
public void onResponse(FlickrResponsePhotos response) {
try {
if(response != null) {
parseFlickrImageResponse(response);
currPage++;
}
} catch (Exception e) {
e.printStackTrace();
showToast("JSON parse error");
}
stopProgress();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Handle your error types accordingly.For Timeout & No connection error, you can show 'retry' button.
// For AuthFailure, you can re login with user credentials.
// For ClientError, 400 & 401, Errors happening on client side when sending api request.
// In this case you can check how client is forming the api and debug accordingly.
// For ServerError 5xx, you can do retry or handle accordingly.
if( error instanceof NetworkError) {
} else if( error instanceof ClientError) {
} else if( error instanceof ServerError) {
} else if( error instanceof AuthFailureError) {
} else if( error instanceof ParseError) {
} else if( error instanceof NoConnectionError) {
} else if( error instanceof TimeoutError) {
}
//mStaggeredView.onRefreshComplete();
stopProgress();
showToast(error.getMessage());
}
});
gsonObjRequest.setTag(TAG_REQUEST);
mVolleyQueue.add(gsonObjRequest);
}
private void showProgress() {
mProgress = ProgressDialog.show(this, "", "Loading...");
}
private void stopProgress() {
isLoading = false;
mListFooter.setVisibility(View.GONE);
mProgress.cancel();
}
private void showToast(String msg) {
Toast.makeText(FlickrActivity.this, msg, Toast.LENGTH_LONG).show();
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
private void parseFlickrImageResponse(FlickrResponsePhotos response) {
FlickrGetImagesResponse photos = response.getPhotos(); //pass array of images to Picture Activity
String[] photoUrl;
photoUrl = new String[photos.getPhotos().size()];
for (int index = 0; index < photos.getPhotos().size(); index++) {
FlickrImage flkrImage = photos.getPhotos().get(index);
photoUrl[index]=flkrImage.getImageUrl();
StaggeredGridViewItem item = null;
item = new GridItem(this, flkrImage,photoUrl); //pass one image of index
mStaggeredView.addItem(item);
}
}
}
Below is the Queue Request program
public class StaggeredDemoApplication extends Application {
private static Context applicationContext;
private static RequestQueue mRequestQueue;
private static ImageLoader mImageLoader;
private static BitmapLruCache mBitmapCache;
public static boolean INIT_FLAG = true;
public void onCreate() {
super.onCreate();
applicationContext = this.getApplicationContext();
mRequestQueue = Volley.newRequestQueue(applicationContext);
long size = Runtime.getRuntime().maxMemory()/4;
mBitmapCache = new BitmapLruCache(50);//(int)size);
mImageLoader = new ImageLoader(mRequestQueue, mBitmapCache);
}
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("RequestQueue not initialized");
}
}
public static ImageLoader getImageLoader() {
if (mImageLoader != null) {
return mImageLoader;
} else {
throw new IllegalStateException("ImageLoader not initialized");
}
}
}
The line that shows error is at FlickrActivity.java
mVolleyQueue = StaggeredDemoApplication.getRequestQueue();
Below is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FlickrActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".FullScreenImageActivity"
android:screenOrientation="portrait" />
</application>
Please advise and help...THank you!
Register your application class name in manifest to avoid Request not initialized issue
<application
android:name=".StaggeredDemoApplication"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
I am relatively new to the android Facebook SDK and need help setting up my login activity after spending fruitless days on this. I am unable to resolve the issues and each time I try and import com.facebook.Session (as recommended in the tutorial, it is automatically changed to "SessionState" instead, which again doesn't help resolve the errors. Please can someone help me fix this.
I am using facebook sdk v3.6 and the latest ADT and SDK tools. Also, my facebook SDK is in a different folder (with the android SDK) and the project exists in a seperate workspace (again, as recommeneded by the tutorial, I did not choose to copy the fb sdk into my workspace, simply imported it). All the FB samples are working fine, I tried copying the code from Scrumptious's main activity to get my login working.
Here's the code for reference:
package com.example.myproject;
import android.content.Intent;
import android.os.Bundle;
import android.service.textservice.SpellCheckerService.Session;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import com.facebook.AppEventsLogger;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
public class FBLoginActivity extends FragmentActivity {
private static final String USER_SKIPPED_LOGIN_KEY = "user_skipped_login";
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private MenuItem settings;
private boolean isResumed = false;
private boolean userSkippedLogin = false;
private UiLifecycleHelper uiHelper;
/* Errors on the next line:
Multiple markers at this line
- Session.StatusCallback cannot be resolved to a type
- Session.StatusCallback cannot be resolved to a type
- Watchpoint:FBLoginActivity [access and modification] -
callback */
private Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
userSkippedLogin = savedInstanceState.getBoolean(USER_SKIPPED_LOGIN_KEY);
}
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.main);
FragmentManager fm = getSupportFragmentManager();
SplashFragment splashFragment = (SplashFragment) fm.findFragmentById(R.id.splashFragment);
fragments[SPLASH] = splashFragment;
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
splashFragment.setSkipLoginCallback(new SplashFragment.SkipLoginCallback() {
#Override
public void onSkipLoginPressed() {
userSkippedLogin = true;
showFragment(SELECTION, false);
}
});
}
#Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
// Call the 'activateApp' method to log an app event for use in analytics and advertising reporting. Do so in
// the onResume methods of the primary Activities that an app may be launched into.
AppEventsLogger.activateApp(this);
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
outState.putBoolean(USER_SKIPPED_LOGIN_KEY, userSkippedLogin);
}
#Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open, try to show the selection fragment
showFragment(SELECTION, false);
userSkippedLogin = false;
} else if (userSkippedLogin) {
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen and ask the user to login, unless the user explicitly skipped.
showFragment(SPLASH, false);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// only add the menu when the selection fragment is showing
if (fragments[SELECTION].isVisible()) {
if (menu.size() == 0) {
settings = menu.add(R.string.settings);
}
return true;
} else {
menu.clear();
settings = null;
}
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.equals(settings)) {
showSettingsFragment();
return true;
}
return false;
}
public void showSettingsFragment() {
showFragment(SETTINGS, true);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
int backStackSize = manager.getBackStackEntryCount();
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
// check for the OPENED state instead of session.isOpened() since for the
// OPENED_TOKEN_UPDATED state, the selection fragment should already be showing.
if (state.equals(SessionState.OPENED)) {
showFragment(SELECTION, false);
} else if (state.isClosed()) {
showFragment(SPLASH, false);
}
}
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
}
The error was resolved by removing the statement
import android.service.textservice.SpellCheckerService.Session;
and instead adding:
import com.facebook.Session;
I am new to android and was trying the Facebook login tutorial on following URl: https://developers.facebook.com/docs/android/scrumptious/authenticate/
I have created app exactly as explained in the tutorial. However, when i try to authenticate using facebook it fails. No error is shown in logcat. Everything seems fine, not even a single error, still its not authenticating.
Here is the code, its exactly same as in tutorial.
package com.l****.n****;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION +1;
private static final String TAG = null;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private boolean isResumed = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
#Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
Log.i(TAG, "Logged in...");
showFragment(SELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
Log.i(TAG, "Logged out...");
showFragment(SPLASH, false);
}
}
}
#Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SPLASH, false);
}
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
#Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
}
Please help as i have no clue what is wrong with the code. Thanks in advance.
P.S: I am running it on real device android version 4.2.2
Check app Id and key hash are correct. If not, i would just download folder into eclipse as an existing android project.
I have a android application through which i have to login to facebook.
I am using the session object of facebook sdk to login.
However the login does not work if the device has the actual facebook application installed in it.
Below is my code
public class FacebookSettings extends FragmentActivity{
/** Called when the activity is first created. */
private static final int LOGIN = 0;
private static final int LOGGED_IN = 1;
private static final int FRAGMENT_COUNT = LOGGED_IN +1;
private Button publishButton;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private Session mSession;
private boolean isResumed = false;
#Override
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
uiHelper.onPause();
isResumed=false;
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
uiHelper.onResume();
isResumed=true;
}
protected static final String LOG_TAG = "FACEBOOK_TEST";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fb_settings);
publishButton=(Button) findViewById(R.id.publishButton);
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
fragments[LOGIN] = fm.findFragmentById(R.id.Login_Fragment);
fragments[LOGGED_IN] = fm.findFragmentById(R.id.Logged_in_Fragment);
android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
for(int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
uiHelper = new UiLifecycleHelper(this, callBack);
uiHelper.onCreate(savedInstanceState);
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
Log.d("facebook","isResumed \n\n\n\n"+state.name());
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
Log.d("facebook","State isOpened in on session state changed");
// If the session state is open:
// Show the authenticated fragment
publishButton.setVisibility(View.VISIBLE);
showFragment(LOGGED_IN, false);
} else if (state.isClosed()) {
Log.d("facebook","State isClosed in on session state changed");
// If the session state is closed:
// Show the login fragment
publishButton.setVisibility(View.INVISIBLE);
showFragment(LOGIN, false);
}
}
}
#Override
protected void onResumeFragments() {
// TODO Auto-generated method stub
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
Log.d("facebook","State isOpened in resume fragments\n\n\n");
publishButton.setVisibility(View.VISIBLE);
showFragment(LOGGED_IN, false);
} else {
// otherwise present the splash screen
// and ask the user to login.
Log.d("facebook","State isClosed in resume fragments\n\n\n");
publishButton.setVisibility(View.INVISIBLE);
showFragment(LOGIN, false);
}
}
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callBack=new StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
// TODO Auto-generated method stub
Log.d("facebook","in status call back \n\n\n\n");
Log.d("facebook","state\n\n\n\n"+session.isClosed());
Log.d("facebook","state\n\n\n\n"+session.isOpened());
Log.d("facebook","state\n\n\n\n"+state.isClosed());
Log.d("facebook","state\n\n\n\n"+state.isOpened());
onSessionStateChange(session,state,exception);
}
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
pls help
thanks
I think you are looking for this :
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/applicationId" />
This metadata is used by the facebook sdk to attach session to your application.
You can also set it while creating a session using the builder
new Session.Builder(this).setApplicationId(Constants.Facebook.APP_ID)
Refer to the AndroidManifest.xml in HelloFacebookSample
I think the problem is that key hash, which you have entered on Facebook page is different from key hash, which is sent by Facebook application. One of the solutions is to add following code in onCreate() method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.facebook.samples.loginhowto",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}
...
Replace com.facebook.samples.loginhowto with your package information. Look in a log for the KeyHash and add it in your developer page.
Additional information you can find on Facebook Developer page facebook.
But remember, this is temporary solution and will most likely work only for account, from which you got key hash with Facebook application.
We have faced the same kind of issue, but it was in IPhone app. When we develop a facbook application in IPhone, login does not work , if have intalled fb application itself.
We resolver it, put Iphone app bundle identifier in which I was created facebook app in facebook developer page.
I hope that this kind of similar feild missing when you was created a fb app for android in db developer page. Please check it.