Android 既存のファイルシステムからプロジェクトを作る

Android

Android 既存のファイルシステムからプロジェクトを作る

  1. EclipseのPackage Explorerの何もないところを右クリックして、Importをクリックします。
  2. Android → Existing Android Code Into Workspace をクリック

General→File Systemだと 「Source is in the hierarchy of the destination.」となってエラーでインポートできません。

  1. Root Directoryと書いてあるところの右のBrowserをクリックし、インポートしたいディレクトリを選択します。
  2. Invalid project description.となってしまう場合は、すでにEclipseのWork spaceに同じ名前のプロジェクトがあるか、そもそも同じWork spaceからインポートしようとしているというケースです。work spaceとは違う場所からimportしましょう。

Android 日本語でフォーマットされた日付を返すサンプルコード

Android
Java

Android 日本語でフォーマットされた日付を返すサンプルコード

Calendarを使って、日本語でフォーマットされた日付(2013年11月21日 11時30分)を返します。
何度か使いそうなので、書いておきます。

 //src内
 public static String getJapaneseDate(Calendar calendar, Context context){
 	
 	int year = calendar.get(Calendar.YEAR);
 	int month = calendar.get(Calendar.MONTH) + 1;
 	int date = calendar.get(Calendar.DATE);
 	int hour = calendar.get(Calendar.HOUR_OF_DAY);
 	int minute = calendar.get(Calendar.MINUTE);
 		
 	String japanese_date = Integer.toString(year) + context.getString(R.string.year) 
 			+ Integer.toString(month) + context.getString(R.string.month) 
 			+ Integer.toString(date) + context.getString(R.string.date) 
 			+ Integer.toString(hour) + context.getString(R.string.hour) 
 			+ Integer.toString(minute) + context.getString(R.string.minute); 
  		
 	return japanese_date;
 		
 }
 //strings.xml内
 <string name="year">年</string>
 <string name="month">月</string>
 <string name="date">日</string>
 <string name="hour">時</string>
 <string name="minute">分</string>

Android 画面がスクロールしない

Android

Android 画面がスクロールしない

Androidの画面を作成していて、画面がはみでる範囲にあるのに、スクロールしない場合があります。

 <ScrollView
       android:id="@+id/ScrollView01"
       android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
 
 …中身
 </ScrollView>

で囲んであげましょう。

このScrollViewを利用する時の注意点があります。

ScrollViewの中には一つしか子要素を入れられません。

ScrollView can host only one direct childというアラートが出ます。
このアラートの解決の仕方はScrollView can host only one direct childを見てください。

 

Android SDKの導入

Android
Eclipse

Android SDKの導入

Eclipse Version: 3.4.1

Androidアプリケーション開発のために、Android SDK(Software Develppment Kit)の導入をします。
Eclipseのプラグインとして導入します。[[:Eclipseの導入は終わっているものとします。

以下、手順です。

  • 下記のサイトから、環境にあったAndroid SDKをダウンロードして解凍します。

http://developer.android.com/sdk/index.html

  • Eclipseのリモートアップデートサイトに、下記のURLを指定します。

https://dl-ssl.google.com/android/eclipse/

  • ダイアログに従い、設定が完了いsたら、一度再起動します。
  • Window→Preference→Androidをクリックします。
  • SDK Locationに先ほどAndroid SDKを解凍した場所を指定します。
  • WindowからAndroid SDK and AVD Managerを選択します。
  • 左側のウィンドウ内のAvailable Packagesをクリックします。
  • 右側に出るリポジトリの選択肢の中から、必要なAPIのバージョンを選択します。すべて選択すると、かなりの容量なので、気をつけましょう。
  • これで完了です。先ほどのAndroid SDK and AVD Managerの、Virtual Devicesというところから、エミュレーターが起動できます。

iPhone、Androidアプリ開発依頼はこちら

Webシステム開発依頼はこちら

Android Serviceがno empty constructorエラーになる

Android

Android IntentServiceがno empty constructorエラーになる

何度かこのエラーに引っかかっている私ですが…

java.lang.RuntimeException: Unable to instantiate service hogehoge.project.services.FetchAddressIntentService: java.lang.InstantiationException: can’t instantiate class hogehoge.project.services.FetchAddressIntentService; no empty constructor

下記のようなエラーが出て、IntentServiceが起動できない時があります。
コンストラクタがないよって言われてますが、下記のように、コンストラクタはありまぁす!

 public class FetchAddressIntentService extends IntentService {
 
    public static final int SUCCESS_RESULT = 0;
    public static final int FAILURE_RESULT = 1;
    public static final String PACKAGE_NAME =
        "smart.location.admin.edison";
    public static final String RECEIVER = PACKAGE_NAME + ".RECEIVER";
    public static final String RESULT_DATA_KEY = PACKAGE_NAME +
        ".RESULT_DATA_KEY";
    public static final String LOCATION_DATA_EXTRA = PACKAGE_NAME +
        ".LOCATION_DATA_EXTRA";
    public static final String TAG = "FetchAddressIntentService";
    
    protected ResultReceiver mReceiver;
 
    public FetchAddressIntentService(String name) {
        super(name);
    }

AndroidManifest.xmlにもサービスを記載してあるし、なんでだよ…っ!!
と思いますが、下記の記事によると

http://stackoverflow.com/questions/11859403/no-empty-constructor-when-create-a-service

引数のないコンストラクタを作らないといけない。

そうです。ふぎゃぁ。

というわけで、上記に下記を追加したら、IntentServiceが起動するようになりました!

    public FetchAddressIntentService() {
        super("FetchAddressIntentService");
    }