2013年12月7日土曜日

Android開発環境のインストール(adt-bundle-windows, a java runtime environment (jre) or Java Development kit (JDK), fail to load the jni shared library)

PCを乗り換えたので、再度Android開発環境を構築してみたので、インストール方法等を紹介します。

今まで、プログラミング経験がない人のためにも、あんまり専門用語は使わずに書いていきます。

とりあえず


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

画面に飛びます。

download sdk クリック


ギガ超えしているので、落とすのに結構時間がかかるかもしれません。。
落とし終えたら、解凍してくだいさい。
zipを右クリック展開でOKです。

そして、解凍したフォルダに行き、『eclipse』→『eclipse.exe』を叩いてください。

Android Developer Toolという画面が立ち上がったら、成功です。

これでひとまず終了



こんな感じのダイアログがでた人は次の作業

JREとJDKを落とします。

JRE
http://java.com/ja/download/

ダウンロード
インストール

JDK
http://www.oracle.com/jp/downloads/index.html
ダウンロード
インストール

そして、環境pathの設定

コントロール パネル\システムとセキュリティ\システム→システムの詳細設定→詳細設定→環境変数→システム環境変数のPathを選択編集,現在書かれていpathの最後に
;を付け加えてjdkのbinまでを記述
C:\Program Files\Java\jdk1.6.0_29\bin
こんな感じ

でもう一回
『eclipse』→『eclipse.exe』を叩いてください。

上手く言った人は終了!!


こんなのが出現した人は失敗
JDKの×64とか×32のインストールを間違えているので、正しい数字でインストールしてください。

以上です。





2013年12月2日月曜日

タブの表示(ActionBar.TabListener, FragmentActivity, TabActivity)

TabActivityが非推奨になったことを最近しりました。。。

結構便利で使っていたのですが。。。

しかし、もっと便利でかっこいいタブを見つけたので紹介します。

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

アクションバーのNAVIGATION_MODE_TABSです。
iPhoneアプリで言うところのUINavigationControllerみたいなものです。

PagerAdapterを使うことによって、スワイプ操作で画面の切り替えができるので
スマホっぽさを演出できます。

使い方は簡単

Androidアプリの新規プロジェクトを作成して、
最低APIのバージョンを11以上にしてください。



この画面でナビゲーションタイプを 『Tabs + Swipe』にすれば完了!!

てか、今まで自分こんな便利なものがあると知らなかった!!
他のもいろいろ試したくなります。

で、作成されたソースを実行すると、
それっぽい感じの画面ができてます。

ということで、

ActionBar.TabListener, TabActivityの使い方は多分だいたいOKだと思いますが、
一応このブログではできるだけ、xmlファイルを使用しないでソースで作成するということなので、
少しだけ、手を加えたサンプルソースを載せます。
まぁ、ほとんどサンプルどおりですが。。。

■サンプルソース

package com.example.tabtest;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    /**
    * The {@link android.support.v4.view.PagerAdapter} that will provide
    * fragments for each of the sections. We use a
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
    * will keep every loaded fragment in memory. If this becomes too memory
    * intensive, it may be best to switch to a
    * {@link android.support.v4.app.FragmentStatePagerAdapter}.
    */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
    * The {@link ViewPager} that will host the section contents.
    */
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
 
        // Set up the ViewPager with the sections adapter.
        mViewPager = new ViewPager(getApplicationContext());
        mViewPager.setId(2);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        setContentView(mViewPager);
 
        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });
 
        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            //アクションバーにタブを設定する
            actionBar.addTab(actionBar.newTab()
            //タブタイトルの設定
            .setText(mSectionsPagerAdapter.getPageTitle(i))
            //リスナーの設定(スワイプ操作を受け付ける)
            .setTabListener(this)
            //アイコンの設定
            setIcon(R.drawable.ic_launcher));
        }
    }


    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    }

    /**
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
    * one of the sections/tabs/pages.
    */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return "1";
            case 1:
                return "2";
            case 2:
                return "3";
            }
            return null;
         }
    }

    /**
    * A dummy fragment representing a section of the app, but that simply
    * displays dummy text.
    */
    public static class DummySectionFragment extends Fragment {

        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
             // Create a new TextView and set its text to the fragment's section
             // number argument value.
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            textView.setText(Integer.toString( getArguments().getInt(ARG_SECTION_NUMBER)));
            return textView;
        }
    }
}

■実行結果


こんな感じになります。

mViewPager = new ViewPager(getApplicationContext());
mViewPager.setId(2);

このViewPagerを作成するところが肝でした。
なぜかって??
setIdをしないと実行エラーになるからです。

あまり普段、id指定をしていなかったので、
結構ハマりました。。。。。。