發表文章

fprintf segmentation fault

記錄一下 遇到segmentation fault:11 的case 以為只是某個陣列出錯,結果居然是fprintf找不到檔案引起的 某個程式區塊內容 char fileloc[50]; sprintf(fileloc,"move_trace/Node%d", i+1); n.PrintTrace(fileloc); //略 void Node::PrintTrace(char *filename){    //open the file    outfile = fopen(filename,"w"); } 其中的outfile是Node的成員變數(對C++不熟,不知道是不是跟Java的講法一樣) FILE *outfile; 之後程式執行到下面這行就出現segmentation fault fprintf(outfile,"#period %d, community %d:\n", i, j); 最後debug出來是因為fopen(filename,"w")並沒有成功,好像目錄"move_trace"不先建立就不行

Java ActionEvent getModifiers()

前些日子學弟來問怎麼判斷按下Button時,是否有同時按下Ctrl+Alt。學弟是在actionPerformed裡,用ActionEvent的getModifiers()與ActionEvent.ALT_MASK和ActionEvent.CTRL_MASK來判斷,不過他執行出來的結果卻是錯的。由於以前也沒有用過getModifiers(),所以花了點時間研究,再加上這過程還發生怎麼在Mac上可以用,但在Win上反而不行的情況,因此在這邊整理了一下學習心得。 首先是getModifiers()的值,我自己遇到的狀況是根據平台以及使用AWT還是Swing的Button而不同,所以debug的時間一直栽在這邊.... Key Mac, Win(Swing JButton) Win(AWT Button) only click, 16 0 with Ctrl, 16|2=18 0|128=128 with Alt, 16|8=24 0|512=512 with Shift, 16|1=17 0|64=64 with Alt+Ctrl, 16|8|2=26 0|512|128=640 得到了Modifiers的值後,接著就是用MASK來比對。如果是上表左邊的情況,使用ActionEvent提供的ALT_MASK和CTRL_MASK等等即可;但如果是右邊的情況,就變成要使用InputEvent的ALT_DOWN_MASK和CTRL_DOWN_MASK等等。使用方法是Modifiers值與mask值的AND運算結果是否等於mask值,就知道Modifiers是否有按下該Modifier。寫成函式就是: private boolean checkModifiers(int modifiers, int mask) { return((modifiers & mask) == mask); } 如何是要判斷是否有兩個Modifier key,例如是Alt+Ctrl,則mask就是這兩個Modifier Mask值的OR運算。 例如 int altWithCtrlMask=ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK; altWithCtrlMask再來跟getModifiers的值進

Build.gradle for Cling UPnP library

最近想試試寫個UPnP app在android上,所以用Cling(http://4thline.org/projects/cling/)這個Library。由於想在Android studio直接套用他的Example,所以參考了原來的pom.xml,在build.gradle裡補足一些參數,就交給Gradle管這些套件了。 最主要還是知道了怎麼設一些變數,以及加入Repositories 以下是build.gradle的設定 apply plugin: 'com.android.application' ext {     jettyVersion = "8.1.8.v20121106"     eslf4jVersion = "1.6.1" } android {     compileSdkVersion 21     buildToolsVersion "21.1.2"      defaultConfig {         applicationId "example.upnplight"         minSdkVersion 16         targetSdkVersion 21         versionCode 1         versionName "1.0"     }     buildTypes {         release {             minifyEnabled false             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'         }     } } repositories {     maven {         url "http://4thline.org/m2"     } } dependencies {     compile 'com.android.support:appcompat-v7:21.0.3'     compile '

Custom Stories with Open Graph

註:當初寫這篇的時候是2014/09/12,那時Facebook sdk還是3.X,現在(2015/06/25)已更新到4.X版,一些方法需要改寫。 舊: 最近在練習寫android app with Facebook SDK, 想用FB的Custom Stories with Open Graph功能, 讓使用者會自動在動態時報(Timeline)上發佈跟這個app的Story, 照著官網Posting using API calls的範例寫, OpenGraphObject book = OpenGraphObject.Factory.createForPost("books.book"); OpenGraphAction readAction = OpenGraphAction.Factory.createForPost("books.reads"); 其中的object("books.book")和action("books.reads")都是FB原本提供的。 想說來用自己定義的story (action是Take ,object是 Bus), 卻不知道createForPost裡的參數分別要填什麼, 填了("take")和("bus")都不行, 找了好久, 注意到books.book(FB提供)和take(自定義)這兩個action的Graph API URL不一樣, 才知道原來要加app的Namespace。 例如這個app的namespace是xx-yy, 那想用自己定義的action和object, 就是要填("xx-yy:take")和("xx-yy:bus")。 更新(2015/06/25): 原本的寫法是OpenGraphObject、OpenGraphAction配合RequestBatch,但到了4.X,這三個類別被替換掉了 而發佈custom stories 也變更簡單 根據官網範例,變成: // Create an object ShareOpenGraphObject object = new ShareOpenGra

Requires ACCESS_MOCK_LOCATION secure setting

紀錄一下: 在執行有使用到Mock Location的app時,出現  E/AndroidRuntime(889): Caused by: java.lang.SecurityException: Requires ACCESS_MOCK_LOCATION secure setting 雖然AndroidManifest裡已有設定權限了,這問題還是存在。    原來使用實體手機驗証時,需要把開發人員選項裡的 "允許模擬位置"打開才行。

compile vlc on android

https://wiki.videolan.org/AndroidCompile/ 紀錄一下遇到的問題: 1. 第一次進行sh compile.sh 時,出現 Archive:  gradle-2.2.1-all.zip   End-of-central-directory signature not found.  Either this file is not   a zipfile, or it constitutes one disk of a multi-part archive.  In the   latter case the central directory and zipfile comment will be found on   the last disk(s) of this archive. unzip:  cannot find zipfile directory in one of gradle-2.2.1-all.zip or         gradle-2.2.1-all.zip.zip, and cannot find gradle-2.2.1-all.zip.ZIP, period. 在資料夾底下 都會有一個未完整的gradle-2.2.1-all.zip,爬了一下文http://metalkin.tistory.com/82 雖然說是韓文...但大概可以猜到怎麼解 修改 compile.sh at Line 81: wget ${GRADLE_URL} 2>/dev/null || curl -O ${GRADLE_URL} =>wget ${GRADLE_URL} 2>/dev/null || curl -O -L ${GRADLE_URL}

Texmaker XeLaTeX設定

圖片
最近把Texmaker 3.3 更新到Texmaker 4.0.2後,原本在快速編譯下是沒有XeLaTeX這個選項的,不知道從那個版本後,Texmaker終於支援了 這樣就可以用比較喜歡的快捷鍵設定來用XeLaTeX編譯(原本的F5用不到,就可以拿來給XeLaTeX) 不然之前都是在自定命令裡設XeLaTeX編譯,那樣就只能用Alt+Shift+F1~5的組合鍵。不過,有時候文章會修改到cite和ref時,就要加入BibTeX編譯,就變成要XeLaTeX編譯+BibTeX編譯+XeLaTeX編譯+XeLaTeX編譯,有時候圖一多,XeLaTeX編譯一次會花不少的時間,所以這樣的組合編譯還是用自定命令會比較省麻煩一點。 既然Texmaker已支援XeLaTeX,自定命令的命令設定也就比較簡單一點了,可以用命令精靈來設定