Home > Tags > Android

Android

Android: "Unable to open sync connection!" の対処例

上記のメッセージはAndroidアプリをEclipseから実機でデバッグする際にDDMS上に出力されたエラーです。

[2010-06-05 15:16:21 - pokeca] Failed to upload pokeca.apk on device '11223344'
[2010-06-05 15:16:21 - pokeca] java.io.IOException 発生: Unable to open sync connection!
[2010-06-05 15:16:21 - pokeca] Launch canceled!

根本原因は不明ですが、対処として下記の手順を試みると解決しました。

  1. コマンドライン上でadb kill-server
  2. Android端末の接続を解除する(USBケーブルを抜く)
  3. コマンドライン上でadb start-server
  4. Android端末をUSBで再接続する。
  5. DDMSで端末が正常に接続されているか確認する。
  6. アプリのデバッグを開始→正常に実行される(OK!)。

コマンド

C:\Users\yukun>adb kill-server

C:\Users\yukun>adb start-server
* daemon not running. starting it now *
* daemon started successfully *

C:\Users\yukun>

5の実行結果

Android端末のUSB接続確認

参考サイト

Windows7 64bitにEclipseでAndroid開発環境をセットアップ

実は今までMac Bookに外部ディスプレイとキーボードを接続してデスクトップで開発していたのですが、Androidのエミュレータを起動していると地味に負荷が連続的にかかって発熱がひどくなってきたので、この度、Windows7 64bitのデスクトップPCを買いました。その際のAndroid開発環境のセットアップ手順を以下に紹介します。

Java 環境のインストール

下記サイトよりJDKをダウンロード&インストール。
Java SE ダウンロード - Sun Developer Network (SDN)
JDK Download 画面
ダウンロードするファイルはWindows x64 と Windows の二つ。それぞれインストールする。
64bit版 はProgram Filesフォルダに、32bit版はProgram Files (x86) フォルダにインストールされる。

C:\>java -version
java version "1.6.0_20"
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)

Android SDK のインストール

下記サイトよりダウンロードする。
Android SDK | Android Developers
解凍後 SDK Setup.exe を実行する。その際に下記のようなエラーが発生した場合は、
画面左部のSettingsからチェックボックス Force https://... sources to be fetched using http;//... をチェックし、使用したいSDK Versionをインストールする。

Failed to fetch URL https://dl-ssl.google.com/android/repository/repository.xml, reason: HTTPS SSL error. You might want to force download through HTTP in the settings.

Eclipse 64bit版のインストール

下記サイトよりダウンロードする。
Eclipse Project Downloads
現在の最新バージョンのリンクをクリックすると下記ページに遷移。
Eclipse Project
画面中部の「Windows (x86_64)」が64bit版Eclipseなので、クリックしてダウンロード。

追記(2010-04-26):上記の手順の中で32bit版のJDK(with JRE)をインストールしているならば、下記の日本語版Eclipse(32bit版)を使用してもよい(ちなみに私はこの方法を採った)。
Pleiades - Eclipse プラグイン日本語化プラグイン

Android Development Tools のインストール

Android Development Tools
Eclipse のプラグインインストール画面で下記のURLを追加し、インストールする。
https://dl-ssl.google.com/android/eclipse/

Android: mksdcardコマンドのabortingの解決法 - could not create file '...', aborting...

下記のコマンドを入力したら、abortされてしまった。

C:\>mksdcard 256M C:\work\sdcard\sdcard.img
could not create file 'C:\work\sdcard\sdcard.img', aborting...

原因はフォルダsdcardを作成してなかった為。SDカードイメージの作成先のフォルダは予め作っておく必要がある。
フォルダ作成後改めてコマンドを入力すると上手くいった。

C:\>mksdcard 256M C:\work\sdcard\sdcard.img

C:\>

Android: リソースの画像ファイルの拡大・縮小描画 - drawBitmap()

画像ファイルの表示(拡大・縮小)

表示する画像はEclipse上でAndroidプロジェクト作成時に自動的に作成されるIcon画像です。
画像パス:プロジェクト名/res/drawable-hdpi/icon.png

resフォルダ以下に置かれたリソースはコンパイル時にプログラムに組み込まれます。その画像リソースを読み込む際は、

Bitmap BitmapFactory.decodeResource(Resources r, int resourcesID)

を用います。読み込んだBitmapインスタンスを描画するには、Canvasクラスのインスタンスメソッドである

void drawBitmap(Bitmap image, int x, int y, Paint p)

を使います。なお、拡大・縮小する場合も上記のdrawBitmapをオーバーロードしたものを使います。

void drawBitmap(Bitmap image, Rect src, Rect dst, Paint p)

今回のサンプルプログラムでは、元画像の幅と高さを2倍したイメージを描画しています。

ImageSp.java

package info.yukun.imagesp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class ImageSp extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(new ImageView(this));
	}
}

ImageView.java

package info.yukun.imagesp;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.view.View;

public class ImageView extends View {

	private Bitmap image;

	public ImageView(Context context) {
		super(context);
		setBackgroundColor(Color.WHITE);

		// リソースの画像ファイルの読み込み
		Resources r = context.getResources();
		image = BitmapFactory.decodeResource(r, R.drawable.icon);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// イメージ描画
		canvas.drawBitmap(image, 0, 0, null);

		int w = image.getWidth();
		int h = image.getHeight();
		// 描画元の矩形イメージ
		Rect src = new Rect(0, 0, w, h);
		// 描画先の矩形イメージ
		Rect dst = new Rect(0, 200, w*2, 200 + h*2);
		canvas.drawBitmap(image, src, dst, null);
	}
}

Android: 10進数→2進数変換アプリ

試しに下図のような簡素な10進2進変換アプリを作ってみました。

以下は書いてみたコード。

ソースコード

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Integer to Binary Calculator</string>
    <string name="label_description">10進数を2進数に変換表示</string>
    <string name="label_integer">10進数:</string>
    <string name="label_binary">2進数:</string>
    <string name="button_convert">変換</string>
</resources>

res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<linearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<textView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/label_description"
    />

<linearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
<textView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label_integer"
    />
<editText android:id="@+id/text_integer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numeric="integer"
    android:maxLength="9"
    android:text=""
    />
</linearLayout>

<button android:id="@+id/button_convert"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="@string/button_convert"
	/>
<textView android:id="@+id/label_result"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text=""
    />
</linearLayout>

ITBCalculatorActivity.java

package info.yukun.android.itbcalc;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ITBCalculatorActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) { // アクティビティが生成される際に必ず呼び出される
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); // UIのレイアウトを設定
    // 指定したリソース(R.java)インデックスからビュー(res/layout/main.xml)内のidのコンポーネント(ここではButton)インスタンスを取得
    Button button = (Button) findViewById(R.id.button_convert);
    button.setOnClickListener(convertToBinary); // ボタンが押された際のイベントを登録
  }

  // 登録するイベントリスナーはView.OnClickListenerを実装
  private View.OnClickListener convertToBinary = new View.OnClickListener() {
    public void onClick(View view) { // ボタンが押されたときに呼び出されるメソッド
      EditText textInteger = (EditText) findViewById(R.id.text_integer); // 入力値が入っているコンポーネントを取得
      String input = textInteger.getText().toString();
      if (input.equals("")) return;
      int intValue = Integer.parseInt(input);
      String binValue = Integer.toBinaryString(intValue); // 10進数整数を2進数文字列に変換
      TextView labelResult = (TextView) findViewById(R.id.label_result); // 結果を表示するTextViewを取得
      labelResult.setText("2進数:" + binValue);
    }
  };
}

Androidアプリを書いてみて

EditText android:id="@+id/text_integer"の属性android:maxLengthを"9"としたのは、変換メソッドInteger.toBinaryString(int value)の引数が32bit整数の為です。本当は属性値を10としてonClickメソッド内で32bitの範囲内(-2 147 483 647 ~ 2 147 483 647 = (2^31) - 1)か否かを判定したほうが良いと思うのですが、今回は端折りました。

ビジュアルエディタやXMLでUIのレイアウトを作っていくというのはFlexのmxmlに似てて取っ掛かり易い感じです。

Home > Tags > Android

バックナンバー
最近のコメント
最近のトラックバック
メタ情報

Return to page top