Android facebook applicationId cannot be null - android

I've been following the following tutorial to integrate my app with Facebook.
Facebook tutorial
I've followed everything on the tutorial, but I've been getting applicationId cannot be null in two cases, and it's really frustrating.
My FacebookActivity onCreate has the following, which is exactly the same as the tutorial:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.main_fb);
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();
}
However when I try to display the activity I get applicationId cannot be null, and the line LogCat points me to is: uiHelper.onCreate(savedInstanceState);
So then I tried commenting out that line, and the activity is displayed. However now when I click on the LoginButton, I get the same error but this time is points me to the applicationId field in the LoginButton class from facebook.
I already have the Id in my string values and my manifest like this:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/APP_ID"/>
I tried getting the Id using code, but nothing changed.
What exactly is causing all this?

TL;DR: you have to write your application's ID in your strings.xml and then reference (i.e. #strings/fb_app_id), because if you put it directly (as value) into AndroidManifest.xml it won't work.
you must define your applicationId in the AndroidManifest.xml
like this:
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/app_id"/>
under <application android:label="#string/app_name".... tag
where app_id is a string within your strings.xml.
sample:
<application android:label="#string/app_name"
android:icon="#drawable/icon"
android:theme="#android:style/Theme.NoTitleBar"
>
<activity android:name=".HelloFacebookSampleActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.facebook.LoginActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar"
android:label="#string/app_name" />
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/app_id"/>
</application>
** please note <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/app_id"/> is within <application> tag
-- and in strings.xml
<string name="app_id">1389xxxxxxxx</string>

Since today the answer is not quite correct.
If someone didn't use this:
AppEventsLogger.activateApp(this);
Since last update you must do it or your app will crashed.
And you also should pass Application here not Context
https://developers.facebook.com/docs/android/getting-started
// Add this to the header of your file:
import com.facebook.FacebookSdk;
public class MyApplication extends Application {
// Updated your class body:
#Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}

The problem is that the id is being converted to integer:
https://code.google.com/p/android/issues/detail?id=78839
In my case the facebook_app_id was being set from the build.gradle file per flavor.
The solution was to wrap the id with ":
flavor.resValue "string", "facebook_app_id", "\"1111111111111\""
or if you would rather avoid escaping:
flavor.resValue "string", "facebook_app_id", '"1111111111111"'

This little code modification at the activity helped me.
#Override
protected void onCreate(Bundle savedInstanceState) {
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getApplication());
super.onCreate(savedInstanceState);
...................
...................
}

Actually you do not have to use flavor codes in gradle...
if you have number longer than 4 Bytes, you should this code in strings.xml
Note: Attention this quotation mark (")
<string name="facebook_app_id">"1111111111111"</string>

Related

In Android manifest android:name shows me error -- parse sdk android

Below is the code code of Android manifest
<application
android:name="com.example.m1.parsedemo.MainActivity" // this line show error
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and the main activity java file is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.enableLocalDatastore(this);
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("31e1ed2c6158fa0d01c3a5201a15e4af3f228a5b")
// if define
.clientKey("7d930e5059ba7ca44ce7083a6708b99eb70d9b4b")
.server("http://35.154.249.137:80/parse/")
.build()
);
ParseUser.enableRevocableSessionInBackground();
ParseObject gameScore = new ParseObject("Manish");
gameScore.put("score", 1337);
gameScore.put("playerName", "Manish");
gameScore.put("cheatMode", false);
gameScore.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if(e == null) {
Log.i("sucess","sucess");
} else {
Log.i("error",e.toString());
}
}
});
}
I am following this guideline -> parse
Removing
android:name=""
shows no error, but cant connect to the server and adding it, show error. I am stuck here cant connect to parse server.
And also please tell me is there any wrong code in main activity java file.
Thanx
You won't need this or you're referencing the wrong file in here:
android:name="com.example.m1.parsedemo.MainActivity" // this line show error
Which you already have:
<activity android:name=".MainActivity"
If you're trying to reference the Application class of your project, use a different name and create another class like calling it App, then:
android:name=".App"
In your AndroidManifest.xml application tag and these codes inside the onCreate() of the App class:
import com.parse.Parse;
import android.app.Application;
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("31e1ed2c6158fa0d01c3a5201a15e4af3f228a5b")
// if define
.clientKey("7d930e5059ba7ca44ce7083a6708b99eb70d9b4b")
.server("http://35.154.249.137:80/parse/")
.build()
);
}
}
Just like the documentation.
This should be name of your application class, not the activity class
<application
android:name="com.example.m1.parsedemo.MainActivity" // this line show error
</application
correct it with the application class which looks like this
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("YOUR_APP_ID")
// if defined
.clientKey("YOUR_CLIENT_KEY")
.server("http://localhost:1337/parse/")
.build()
);
}
}

my android application doesnt run?

I was trying to make application, it was working but now when I run it in emulator it's not even giving me the welcome screen. Code:
public class TestArabActivity extends Activity {
/** Called when the activity is first created. */
TextView tvTop,tvBottom;
TableLayout tlStart;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_screen);
//init variables
initVars();
}
private void initVars(){
tvTop = (TextView) findViewById(R.id.tv_start_TopTittle);
tvBottom = (TextView) findViewById(R.id.tv_strat_BottomTittle);
tlStart = (TableLayout) findViewById(R.id.tl_start);
}
}
I used debugger and saw these:
the value of savedInstanceState = null
values of tvTop, tvBottom and tlStart are null also
Why those values are null? I know this is the problem.
Anyone know how to solve it? Thanks.
Use DDMS to debug your application,if it throws any exception, you can see from the stack trace where the problem comes from. if it does not throw any exception. then probably you forgot to declear intent-filter in the menifest:
<activity android:name="TestArabActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

New activity (screen) and "Application has stopped..."

in my app I am trying to add new screen. In my activity I've:
public void addItem(View v) {
Intent i = new Intent(SQLiteListActivity.this, add_screen.class);
startActivity(i);
}
In add_screen.java:
public class add_screen extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
In file add_screen.xml I've layout this screen. In file AndroidManifest.xml I added this next activity, especially:
<activity class=".Add_screen" android:name="ADD_ITEM" android:label="Add item">
</activity>
I am still getting an error message about "Application has been stopped." I am newbie in Android development, I tried to do this by some tutorial, I've everything by it, but I don't know why, I'm still getting the error message above...
Can you help me, please, with this problem? I've no idea, what could be wrong.
Try replacing the activity tag in your manifest with this instead:
<activity
android:name=".add_screen"
android:label="Add item"
>
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You shouldn't need the class attribute, I don't even know if that's recognized.
Edit: Try the above instead?

Android search from searchable activity

I have an Activity that holds a list. Via the Android onSearchRequested() I implemented a search.
The results are shown as a list with the same adapter in another Activity. Working fine so far.
Also, I want to be able to search from that second Activity showing the new results in the same list.
My AndroidManifest.xml for the two activities:
<activity android:name=".ListActivity" android:label="List">
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity" android:label="Results">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/search" android:value=".SearchActivity" />
</activity>
The SearchActivity's onResume():
#Override
protected void onResume() {
super.onResume();
Intent queryIntent = getIntent();
String value = queryIntent.getStringExtra(SearchManager.QUERY);
setView(value);
}
The setView() method does a foreach loop through all objects adding them to a result-array which is used for a new Adapter that the list shows.
ca = new CustomAdapter(this, R.layout.customadapter, resultArray);
list.setAdapter(pa);
list.invalidate();
When trying to search from the second Activity the search bar appears, I can enter my search value, send it - but the list doesn't change (and even the keyboard stays).
What's missing?
Edit: Tried to make it easier to understand.
Found that question which describes kind of the same problem.
Instead of overriding onResume() I have to override onNewIntent()
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String value = intent.getStringExtra(SearchManager.QUERY);
setView(value);
}

Android preferences problem

I am following this tutorial: link text
Preferences.java:
public class Preferences extends PreferenceActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
PreferencesTutorial.java:
public class PreferencesTutorial extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button prefBtn = (Button) findViewById(R.id.prefButton);
prefBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent settingsActivity = new Intent(getBaseContext(),
Preferences.class);
startActivity(settingsActivity);
}
});
}
}
Preferences.xml:
When application starts, and i click the prefButton, an error occures: "The application PreferencesTutorial (process PreferencesTutorial.com.examples) has stopped unexpectedly. Please try again"
I haven't found any mistakes in the code.
I would also like to show my filestructure if that helps:
AndroidManifest.xml:
What is wrong with the code?
Even if i add (where the cursor is)
<activity
android:name=".Preferences"
android:label="#string/set_preferences">
</activity>
i still get the error.
Try removing this import, if you have it;
import java.util.prefs.Preferences;
You have to mention this in your androidManifest.xml file
<activity
android:name=".Preferences"
android:label="#string/set_preferences">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You probably do not have Preferences defined in your manifest.
However, as others have indicated, use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and see the stack trace associated with your crash.
Is the error raised in the OnClick in PreferencesTutorial Class or onCreate in the preferences Class? Stick a couple of Log.d("Debug","%ID") in various locations and see which one doesn't get called.

Categories

Resources