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

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

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

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

Android

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

Android Studio

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

Q&A

解決済

1回答

780閲覧

Android Studio/Button idを参照してもnullで返ってくる

nmnm789

総合スコア1

Java

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

Android

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

Android Studio

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

0グッド

0クリップ

投稿2021/05/17 12:04

編集2021/05/18 03:47

前提・実現したいこと

Android Studio初心者で、お絵描きアプリを作ろうと思っています。

黒、青、赤の3色のボタンを用意し、押した後にペンが該当色に変わるようにしようとしています。
ボタンを画面に表示するところで、MainActivityクラスのsetContentViewメソッドの変数に描画クラスMyViewを指定したところ、宣言の段階でボタンの値がnullで返ってきてしまい、うまく画面に表示できません。

xmlのボタンの記述は、MyViewをxmlに登録し、その中で行っています。
3色のボタンを指定し表示させるためにはどうすればよろしいでしょうか。

[追記]
今目指したいDesign画面は次のようになります。

該当のソースコード

MainActivity

java

1import androidx.appcompat.app.AppCompatActivity; 2import android.os.Bundle; 3import android.view.View; 4import android.widget.Button; 5 6public class MainActivity extends AppCompatActivity implements View.OnClickListener { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(new MyView(this)); 12 Button bBlack = (Button) findViewById(R.id.bBlack); 13 Button bBlue = (Button) findViewById(R.id.bBlue); 14 Button bRed = (Button) findViewById(R.id.bRed); 15 bBlack.setOnClickListener(this); 16 } 17 18 @Override 19 public void onClick(View view) { 20 // 21 } 22}

activity_main

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <[パッケージ名クラス名].MyView 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 tools:ignore="MissingConstraints" 13 tools:layout_editor_absoluteX="0dp" 14 tools:layout_editor_absoluteY="0dp"> 15 16 <Button 17 android:id="@+id/bBlack" 18 android:layout_width="50dp" 19 android:layout_height="wrap_content" 20 android:layout_marginStart="28dp" 21 android:layout_marginBottom="28dp" 22 android:background="#000000" 23 app:layout_constraintBottom_toBottomOf="parent" 24 app:layout_constraintStart_toStartOf="parent" /> 25 26 <Button 27 android:id="@+id/bRed" 28 android:layout_width="50dp" 29 android:layout_height="wrap_content" 30 android:layout_marginBottom="28dp" 31 android:background="#FF0000" 32 app:layout_constraintBottom_toBottomOf="parent" 33 app:layout_constraintStart_toEndOf="@+id/bBlue" /> 34 35 <Button 36 android:id="@+id/bBlue" 37 android:layout_width="50dp" 38 android:layout_height="wrap_content" 39 android:layout_marginBottom="28dp" 40 android:background="#0000FF" 41 app:layout_constraintBottom_toBottomOf="parent" 42 app:layout_constraintStart_toEndOf="@+id/bBlack" /> 43 </[パッケージ名].MyView> 44 45</androidx.constraintlayout.widget.ConstraintLayout>

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

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

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

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

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

hoshi-takanori

2021/05/17 13:56

とりあえず setContentView(new MyView(this)); を setContentView(R.layout.activity_main); にする必要がありますが、MyView は ViewGroup のサブクラスではないので、今度は MyView の中に Button を作ることはできないというエラーになりますね。どういう画面レイアウトにしたいのでしょうか?
nmnm789

2021/05/18 02:25

ご返信いただきありがとうございます。 ViewのサブクラスであるMyViewで描画した画面を表示し、その上にボタンを置くレイアウトにしようと考えていたのですが、難しいでしょうか。 初歩的なことで申し訳ございません。
guest

回答1

0

ベストアンサー

とりあえずこんな感じでいかがでしょうか。

MainActivity.java

diff

1- setContentView(new MyView(this)); 2+ setContentView(R.layout.activity_main);

activity_main.xml

  • Button を MyView の子ビューにする必要はないと思います。
  • Button の色は backgroundTint で指定できます。

xml

1<?xml version="1.0" encoding="utf-8"?> 2<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".MainActivity"> 8 9 <[パッケージ名].MyView 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 tools:ignore="MissingConstraints" 13 tools:layout_editor_absoluteX="0dp" 14 tools:layout_editor_absoluteY="0dp" /> 15 16 <Button 17 android:id="@+id/bBlack" 18 android:layout_width="50dp" 19 android:layout_height="wrap_content" 20 android:layout_marginStart="28dp" 21 android:layout_marginBottom="28dp" 22 android:backgroundTint="#000000" 23 app:layout_constraintBottom_toBottomOf="parent" 24 app:layout_constraintStart_toStartOf="parent" /> 25 26 <Button 27 android:id="@+id/bRed" 28 android:layout_width="50dp" 29 android:layout_height="wrap_content" 30 android:layout_marginBottom="28dp" 31 android:backgroundTint="#FF0000" 32 app:layout_constraintBottom_toBottomOf="parent" 33 app:layout_constraintStart_toEndOf="@+id/bBlue" /> 34 35 <Button 36 android:id="@+id/bBlue" 37 android:layout_width="50dp" 38 android:layout_height="wrap_content" 39 android:layout_marginBottom="28dp" 40 android:backgroundTint="#0000FF" 41 app:layout_constraintBottom_toBottomOf="parent" 42 app:layout_constraintStart_toEndOf="@+id/bBlack" /> 43 44</androidx.constraintlayout.widget.ConstraintLayout>

投稿2021/05/18 02:58

編集2021/05/18 02:59
hoshi-takanori

総合スコア7895

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問