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

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

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

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

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

Android

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

Android Studio

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

Q&A

解決済

1回答

1148閲覧

Android Studioでコンパイルエラー (変数の初期化)

Yakusugi

総合スコア123

XML

XMLは仕様の1つで、マークアップ言語群を構築するために使われています。

Java

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

Android

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

Android Studio

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

0グッド

1クリップ

投稿2021/01/14 12:40

Android Studioで、教材を参考に簡単なIF文で条件分岐するアプリを作成してみたのですが、変数の初期化でコンパイルエラーになってしまいます。
MainActivity.javaの55/56行目のageとjobDescriptionでカーソルを合わせると、「Valiable age might not have been initialized」と表示されます。
しかし、16行目の「EditText age, jobDescription;」と28/29行目でそれぞれ「age = findViewById(R.id.age);」のように宣言と初期化を行っているのですが、何故コンパイルエラーになるのかが原因が突き止められません。
大変お手数ではございますが、ご回答頂けますと幸いです。

MainActivity.java

package com.example.csinjava; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText edtFN, edtSN; Button btnCalculate; EditText age, jobDescription; Button btnAssessment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edtFN = findViewById(R.id.editFN); edtSN = findViewById(R.id.editSN); btnCalculate = findViewById(R.id.btnCalculate); age = findViewById(R.id.age); jobDescription = findViewById(R.id.jobDescription); btnAssessment = findViewById(R.id.btnAssessment); boolean myLaptopIsMacbook = false; if (myLaptopIsMacbook == true) { Log.i("MyTag", "Yes, it is true"); } else if (myLaptopIsMacbook != true) { Log.i("MyTag", "No, it is true"); } } public void calculateButtonPressed(View buttonView) { int firstNumber = Integer.parseInt(edtFN.getText().toString()); int secondNumber = Integer.parseInt(edtSN.getText().toString()); if (firstNumber > secondNumber) { btnCalculate.setText("First number is greater than the Second number"); } else if (firstNumber < secondNumber) { btnCalculate.setText("Second number is greater than the First number"); } else if (firstNumber == secondNumber) { btnCalculate.setText("Equal"); } } public void assessBtnPressed(View buttonView) { int age = Integer.parseInt(age.getText().toString()); String jobDescription = jobDescription.getText().toString(); if (age >= 18 && jobDescription == "Android Developer") { btnCalculate.setText("You're quoalified"); } else if (age < 18 && jobDescription == "Android Developer") { btnCalculate.setText("You're not quoalifiedr"); } else if (age >= 18 && jobDescription != "Android Developer") { btnCalculate.setText("You're not quoalifiedr"); } } }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/editFN" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="85dp" android:ems="10" android:hint="Please enter the first number..." android:inputType="textPersonName" android:onClick="calculateButtonPressed" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editSN" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="472dp" android:ems="10" android:hint="Please enter the second number..." android:inputType="textPersonName" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/btnCalculate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="365dp" android:onClick="calculateButtonPressed" android:text="Calculate" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <EditText android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="404dp" android:ems="10" android:hint="Type your age" android:inputType="textPersonName" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/jobDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="502dp" android:ems="10" android:hint="Write your job title" android:inputType="textPersonName" android:text="Name" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnAssessment" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="606dp" android:onClick="assessBtnPressed" android:text="Assessment" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>

イメージ説明

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

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

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

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

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

guest

回答1

0

ベストアンサー

MainActivity.java の 16 行目で age, jobDescription というフィールドが定義されてますが、55/56 行目で同じ名前のローカル変数を宣言していて、例えば age.getText().toString() の age がローカル変数の方を指すとコンパイラが判断するためにエラーになります。修正するには、どちらかの変数名を変えるか、

java

1 int age = Integer.parseInt(this.age.getText().toString()); 2 ~~~~~

のようにフィールドの頭に this. をつけると良いでしょう。

投稿2021/01/14 13:31

hoshi-takanori

総合スコア7895

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

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

Yakusugi

2021/01/14 13:41

ありがとうございます。解決しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問