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

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

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

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

Android

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

Android Studio

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

Q&A

解決済

1回答

1399閲覧

(AutoFitTextureView) findViewById(R.id.texture);//nullになります。

giant

総合スコア132

Java

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

Android

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

Android Studio

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

0グッド

0クリップ

投稿2017/09/08 09:57

(AutoFitTextureView) findViewById(R.id.texture);//nullになります。
何故なのでしょうか?

xmlファイルでは、このように指定しています。

xml

1<com.google.android.exoplayer2.demo.AutoFitTextureView 2 android:id="@+id/texture" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 android:layout_alignParentBottom="true" 6 android:layout_alignParentStart="true" 7 android:layout_alignParentTop="true" /> 8

AutoFitTextureViewのjavaクラスです。

java

1package com.google.android.exoplayer2.demo; 2 3import android.content.Context; 4import android.util.AttributeSet; 5import android.view.TextureView; 6 7import android.content.Context; 8import android.util.AttributeSet; 9import android.view.TextureView; 10 11public class AutoFitTextureView extends TextureView { 12 13 private int mRatioWidth = 0; 14 private int mRatioHeight = 0; 15 16 public AutoFitTextureView(Context context) { 17 this(context, null); 18 } 19 20 public AutoFitTextureView(Context context, AttributeSet attrs) { 21 this(context, attrs, 0); 22 } 23 24 public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) { 25 super(context, attrs, defStyle); 26 } 27 28 /** 29 * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio 30 * calculated from the parameters. Note that the actual sizes of parameters don't matter, that 31 * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. 32 * 33 * @param width Relative horizontal size 34 * @param height Relative vertical size 35 */ 36 public void setAspectRatio(int width, int height) { 37 if (width < 0 || height < 0) { 38 throw new IllegalArgumentException("Size cannot be negative."); 39 } 40 mRatioWidth = width; 41 mRatioHeight = height; 42 requestLayout(); 43 } 44 45 @Override 46 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 47 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 48 int width = MeasureSpec.getSize(widthMeasureSpec); 49 int height = MeasureSpec.getSize(heightMeasureSpec); 50 if (0 == mRatioWidth || 0 == mRatioHeight) { 51 setMeasuredDimension(width, height); 52 } else { 53 if (width < height * mRatioWidth / mRatioHeight) { 54 setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); 55 } else { 56 setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); 57 } 58 } 59 } 60 61} 62 63

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

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

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

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

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

guest

回答1

0

ベストアンサー

findViewById()を呼び出しているのは、setContentView()によってXMLに記述したレイアウトをActivityに描画した後になっているのでしょうか。順序が逆ならばnullが返るでしょう。

投稿2017/09/08 11:30

keicha_hrs

総合スコア6766

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

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

giant

2017/09/09 01:20

ありがとうございました。エラーが消えました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問