Browsing 7239 questions and answers with Jon Skeet
I'm curious as to why the following line works fine in IntelliJ but is shown as an error in Eclipse (both using Java 7):
return toolbar.getClientProperty("PrototypeToolbar") ==...
It looks like this was valid code under javac 1.7, but isn't under 1.8. I suspect it's a bug fix in javac.
This isn't treating 0 as false and anything non-0 as true. It's effectively doing this:
Object wrapped = true; //...
more
1/5/2016 5:12:46 PM
I have the below markup, in which each item element must contain exactly one a, b, c, or d element, and in addition can contain a variety of additional elements. The position of...
Well it sounds like you might want something like:
var names = new XName[] { "a", "b", "c", "d" };
var element = xmlDoc.Root
.Elements("item")
.Elements()
.FirstOrDefault(x =>...
more
1/5/2016 3:50:50 PM
public class OverLoad {
void method(Integer i){
System.out.println("Integer "+i);
}
void method(int i){
System.out.println("in "+i);
}
public...
By default it going into primitive data type
Well yes, because that's the exact type of the argument. The type of the literal 2 is int, not Integer (JLS 3.10.1). It's convertible to Integer via a boxing conversion (JLS 5.1.7), but...
more
1/5/2016 7:45:09 AM
I am trying to integrate Android application with facebook. I am following these tutorials:
https://developers.facebook.com/docs/android/getting-started
http://code.tutsplus.com/tutorials/quick-tip-add-facebook-login-to-your-android-app--cms-23837
But I am facing the following problems :
01-05 12:52:45.325 463-463/socialmediaintegration.arifhasnat.com.facebooklogin E/AndroidRuntime: FATAL EXCEPTION: main
Process: socialmediaintegration.arifhasnat.com.facebooklogin, PID: 463
java.lang.ExceptionInInitializerError
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at socialmediaintegration.arifhasnat.com.facebooklogin.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5293)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
Caused by: null
at com.facebook.internal.Validate.sdkInitialized(Validate.java:99)
at com.facebook.FacebookSdk.getCallbackRequestCodeOffset(FacebookSdk.java:735)
at com.facebook.internal.CallbackManagerImpl$RequestCodeOffset.toRequestCode(CallbackManagerImpl.java:109)
at com.facebook.login.widget.LoginButton.<clinit>(LoginButton.java:58)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at socialmediaintegration.arifhasnat.com.facebooklogin.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5293)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:830)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:646)
at dalvik.system.NativeStart.main(Native Method)
Here is my code:
MainActivity.java
package socialmediaintegration.arifhasnat.com.facebooklogin;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
public class MainActivity extends AppCompatActivity {
private TextView info;
private LoginButton loginButton;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
info = (TextView)findViewById(R.id.info);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
info.setText(
"User ID: "
+ loginResult.getAccessToken().getUserId()
+ "\n" +
"Auth Token: "
+ loginResult.getAccessToken().getToken()
);
}
@Override
public void onCancel() {
info.setText("Login attempt canceled.");
}
@Override
public void onError(FacebookException e) {
info.setText("Login attempt failed.");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="socialmediaintegration.arifhasnat.com.facebooklogin" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.INTERNET"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:label="@string/app_name" />
<provider android:authorities="com.facebook.app.FacebookContentProvider1234"
android:name="com.facebook.FacebookContentProvider"
android:exported="true" />
</application>
</manifest>
layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/info"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="18sp"
/>
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>
And gradle dependencies :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "socialmediaintegration.arifhasnat.com.facebooklogin"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.facebook.android:facebook-android-sdk:4.0.0'
}
I suspect the problem is that you're initializing the SDK too late. From the tutorial you linked to:
// Initialize the SDK before executing any other operations,
// especially, if you're using Facebook UI elements.
The exception appears...
more
1/5/2016 7:24:27 AM
Today I stumbled about some strange inner (non-static) class behaviour.
If I have the following classes ...
class B {
String val = "old";
void run(){
val =...
This line:
new InheritedB().printVal();
creates a new instance of InheritedB, whose containing instance is the existing instance of B (where val is "new"). But at this point there are two val variables:
The one in the existing...
more
1/4/2016 2:24:37 PM
why we use setInt with select query instead of using getInt when value is already there in database?
try {
conn = getConnection();
ps =...
You're setting the value of the parameter to be used in the query. The ? in the SQL represents the parameter, and here you're giving it a value.
When you call getString() later, that's getting a value from the results of the query, which...
more
1/4/2016 12:58:02 PM
For example I have some array: byte[,] arr = new byte[4800, 3000]; and I need to get part of that array starting from 512, 600 and ending at 1024, 1200.
How I can do that...
You can use Buffer.BlockCopy to copy a block of contiguous memory from one array to another. For example:
// Names changed to be more conventional
int originalHeight = ...; // Original "height" in array
int newHeight = endY - startY;
for...
more
1/4/2016 11:30:53 AM
I used spring boot to develop a shell project used to send email, e.g.
sendmail -from foo@bar.com -password foobar -subject "hello world" -to aaa@bbb.com
If the from and...
Well, it sounds like you're trying to check whether the "nullity" condition of the two is the same or not. You could use:
if ((from == null) != (password == null))
{
...
}
Or make it more explicit with helper variables:
boolean...
more
1/4/2016 6:59:53 AM
I'm currently serializing HighScoreData from a C# application to an XML file using the XmlSerializer namespace. This is producing an inconsistent result regarding the outputted...
This is the problem:
FileStream stream = File.Open(fullpath, FileMode.OpenOrCreate);
That doesn't truncate the file if it already exists - it just overwrites the data that it writes, but if the original file is longer than the data...
more
1/3/2016 7:47:04 PM
I'm working with GZipStream at the moment using .net 3.5.
I have two methods listed below. As input file I use text file which consists of chars 's'. Size of the file is 2MB. This...
You're trying to decompress each "chunk" as if it's a separate gzip file. Don't do that - just read from the GZipStream in a loop:
using (var fsout = File.Create(fileOut))
{
using (var fsIn = File.OpenRead(fileIn))
{
using...
more
1/3/2016 8:35:43 AM