質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

2回答

5004閲覧

onCreate部分などのコードが実行されない

sakuma_yujin

総合スコア27

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2017/03/28 03:56

編集2017/03/28 04:09

###前提・実現したいこと
onCreateの中にあるコードが実行されません。
onClickなども同様です
ページは横スワイプ画面のページの中の一つです

実行自体はできてレイアウトファイルの定義通りに表示はされましたが処理が行われません
どうすれば良いのでしょうか

コードは「このページ」のものです
具体的に教えていただけるとありがたいです
よろしくお願いします

###発生している問題・エラーメッセージ

特にありませんが指定した処理が実行されません

###該当のソースコード
page1.java(処理が通らない部分のコード >印部分)

java

1 2 3import android.content.Intent; 4import android.content.SharedPreferences; 5import android.os.Bundle; 6import android.support.v7.app.AppCompatActivity; 7import android.util.Log; 8import android.view.View; 9import android.widget.ImageButton; 10import android.widget.TextView; 11 12public class page1 extends AppCompatActivity implements View.OnClickListener { 13private ImageButton imageButton; 14 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_page1); 18 >Log.d("debug","W"); ・・・・・* 19 >imageButton=(ImageButton)findViewById(R.id.imageButton4); 20 >imageButton.setOnClickListener(this); 21 22 23 >final String PREFERRENCES_FILE_NAME = "PrefrencesFile"; 24 >//プリファレンスのインスタンスを取得 25 >SharedPreferences pre = this.getSharedPreferences(PREFERRENCES_FILE_NAME, MODE_PRIVATE); 26 >//デフォルトプリファレンスのインスタンスを取得する 27 >//SharedPreferences pre = >PreferenceManager.getDefaultSharedPreferences(this); 28 >//読み込み 29 >String str = pre.getString("NAME", "NONE"); 30 >TextView text = (TextView) this.findViewById(R.id.name); 31 >text.setText(str); 32 33 } 34 public void onClick(View v) { 35 >Log.d("debug","g"); ・・・・・* 36 >if (v == imageButton) { 37 > Intent intent = new Intent(this, Setting.class); 38 > startActivityForResult(intent, 0); 39 } 40 } 41 42} 43

activity_page1.xml(page1.javaのレイアウトファイル)

xml

1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6android:paddingBottom="@dimen/activity_vertical_margin" 7android:paddingLeft="@dimen/activity_horizontal_margin" 8android:paddingRight="@dimen/activity_horizontal_margin" 9android:paddingTop="@dimen/activity_vertical_margin" 10tools:context="com.example.syoribuuser.firstopenandfragment.page1" 11android:background="@drawable/back"> 12 13<ImageView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:id="@+id/imageView" 17 android:background="@drawable/tnm" 18 android:layout_alignParentRight="true" 19 android:layout_alignParentEnd="true" 20 android:layout_alignParentLeft="true" 21 android:layout_alignParentStart="true" /> 22 23<TextView 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="New Text" 27 android:id="@+id/name" 28 android:layout_marginTop="39dp" 29 android:textSize="25dp" 30 android:layout_alignParentTop="true" 31 android:layout_centerHorizontal="true" /> 32 33<TextView 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:text="Todays Plan....." 37 android:id="@+id/textView2" 38 android:textSize="35dp" 39 android:textStyle="normal|italic" 40 android:theme="@style/AlertDialog.AppCompat.Light" 41 android:layout_below="@+id/Date" 42 android:layout_centerHorizontal="true" /> 43 44<ImageButton 45 android:layout_width="100px" 46 android:layout_height="100px" 47 android:id="@+id/imageButton" 48 android:background="@drawable/unnamed" 49 android:layout_above="@+id/imageButton3" 50 android:layout_alignParentRight="true" 51 android:layout_alignParentEnd="true" 52 android:adjustViewBounds="true" /> 53 54<ImageButton 55 android:layout_width="100px" 56 android:layout_height="100px" 57 android:id="@+id/imageButton2" 58 android:background="@drawable/icon256" 59 android:layout_alignParentBottom="true" 60 android:layout_alignParentRight="true" 61 android:layout_alignParentEnd="true" /> 62 63<ImageButton 64 android:layout_width="100px" 65 android:layout_height="100px" 66 android:id="@+id/imageButton3" 67 android:background="@drawable/facebook" 68 android:layout_above="@+id/imageButton2" 69 android:layout_alignParentRight="true" 70 android:layout_alignParentEnd="true" /> 71 72<ImageButton 73 android:layout_width="100px" 74 android:layout_height="100px" 75 android:id="@+id/voice" 76 android:background="@android:drawable/presence_audio_away" 77 android:layout_above="@+id/imageButton" 78 android:layout_alignParentRight="true" 79 android:layout_alignParentEnd="true" 80 android:nestedScrollingEnabled="false" /> 81 82<ImageButton 83 android:layout_width="wrap_content" 84 android:layout_height="wrap_content" 85 android:id="@+id/imageButton4" 86 android:layout_alignBottom="@+id/imageButton2" 87 android:layout_alignParentLeft="true" 88 android:layout_alignParentStart="true" 89 android:src="@android:drawable/ic_menu_preferences" 90 android:layout_below="@+id/imageButton3" 91 android:layout_toStartOf="@+id/textView2" 92 android:layout_toLeftOf="@+id/textView2" /> 93 94 <TextView 95 android:layout_width="wrap_content" 96 android:layout_height="wrap_content" 97 android:id="@+id/Date" 98 android:textSize="35dp" 99 android:textStyle="normal|italic" 100 android:theme="@style/AlertDialog.AppCompat.Light" 101 android:layout_below="@+id/name" 102 android:layout_toRightOf="@+id/imageButton4" 103 android:layout_toEndOf="@+id/imageButton4" 104 android:text="22222222" /> 105 106</RelativeLayout> 107

Main3activity.java(スワイプを動かしているコード)

java

1 2 3 4import android.app.Activity; 5import android.content.Context; 6import android.os.Bundle; 7import android.support.v4.view.PagerAdapter; 8import android.support.v4.view.ViewPager; 9import android.view.LayoutInflater; 10import android.view.View; 11import android.view.ViewGroup; 12 13public class Main3Activity extends Activity { 14 @Override 15 public void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main3); 18 19 ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager); 20 PagerAdapter mPagerAdapter = new MyPagerAdapter(); 21 mViewPager.setAdapter(mPagerAdapter); 22 23 24 } 25 26 private class MyPagerAdapter extends PagerAdapter { 27 @Override 28 public Object instantiateItem(ViewGroup container, int position) { 29 // レイアウトファイル名を配列で指定します。 30 int[] pages = {R.layout.activity_page1, R.layout.activity_page2, R.layout.activity_page3}; 31 32 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 33 34 View layout; 35 layout = inflater.inflate(pages[position], null); 36 ((ViewPager) container).addView(layout); 37 return layout; 38 } 39 40 @Override 41 public void destroyItem(ViewGroup container, int position, Object object) { 42 ((ViewPager) container).removeView((View) object); 43 } 44 45 @Override 46 public int getCount() { 47 // ページ数を返します。 48 return 3; 49 } 50 51 @Override 52 public boolean isViewFromObject(View view, Object object) { 53 return view.equals(object); 54 } 55 } 56 57} 58 59

###試したこと
Log.d()により処理の通過を調べましたがlogcatへの表示が確認できませんでした(page1.javaの「・・・・・*」部分)

###補足情報(言語/FW/ツール等のバージョンなど)
java
androidstudio
xml

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

page1はアクティビティですね。
立ち上げたのはMain3Activityだけではありませんか?


更新
コメントから、個人的にですが初めての方がブログなりで学習する場合。
まず一つの場所で通して学習ができるような所でまずやってみる事をおすすめします。

と言う処で
Androidであればまずこちらを。
Building Your First App | Android Developers
公式のチュートリアルです。
自身が分かる部分についてはざっと目を通して「次へ」で進んでいいですので目は通してください。
公式サイトには他にも様々な情報がありますし英語に関してはChromeの自動翻訳なりだけでも分かる程度のものですので難しく考えずに見てみる事から始めてみると良いと思います。

それと、さっと検索した所で
Tech Institute
ちょっと古いですが日本語なのと各資料が無料でDLできるようですので、Androidの基本を学ぶ分には良いと思います記載しておきますね。

投げちゃう感じになってしまい申し訳ないですが、
急がず無理せずでご参考になれば幸いです。

投稿2017/03/28 04:10

編集2017/03/28 06:06
uniko

総合スコア448

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakuma_yujin

2017/03/28 04:29

他のところからMain3Activity呼び出しています。 Main3Activityがよびだされてpage1、2、3が表示されているという解釈なのですが違うのでしょうか
uniko

2017/03/28 04:39 編集

page.javaはActivityですので、Activityの中にActivityは表示できません。その場合はfragmentを利用してください。 その場合はPagerAdapterもFragmentPagerAdapter等になります。 なおMain3Activityではpager内でviewをinflateしていますのでただpage1,2,3のレイアウトを表示しているだけになっちゃっています。
sakuma_yujin

2017/03/28 04:54

このままではpageの上で処理を実行できないということですか? Fragmentだとどうしても使いにくくてActivityで動かせるものを探してきたのですが・・・
uniko

2017/03/28 05:04

MyPagerAdapterの、 View layout; layout = inflater.inflate(pages[position], null); 等で変数layoutのviewに対し各処理をつけたりすればできます。 が、androidの基本は知っていないと厳しいと思われます。 失礼ですが、androidのプログラミングについては書籍か何かの参考書等で一通りの勉強はしましたか?それともブログを見たりでやっていますでしょうか?
sakuma_yujin

2017/03/28 05:48 編集

ブログを見たり、という感じです。 やはり厳しそうでしょうか かなり長いコードになりますか?
uniko

2017/03/28 05:47

なるほど、初めはみなさんそうですので気張らずに。 ちょっと回答を更新します。
sakuma_yujin

2017/03/28 06:09 編集

ありがとうございます これから少しづつ勉強していきたいと思います
uniko

2017/03/28 06:13

いえ、一応先ほどの質問についてですが、 変数layoutに対しfindByIdでほしいViewを取得し処理をつけていく形です。 今回だとpage.javaのonCreateのご自身が付けている部分をつけていく形ですね。 ご参考まで。
guest

0

レイアウトファイルの名前とActivityの名前を揃えると、レイアウトファイルを読み込むと勝手にActivityが呼ばれると勘違いしていませんか?
レイアウトファイルの名前とActivityの名前は無関係です。

投稿2017/03/28 04:51

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問