setContentView(R.layout.activity_main); の R に、Cannot resolve symbol 'R'となります。
java
1package com.example.android.sample.calculator; 2 3import android.support.v7.app.AppCompatActivity; 4import android.os.Bundle; 5 6public class MainActivity extends AppCompatActivity { 7 8 @Override 9 protected void onCreate(Bundle savedInstanceState) { 10 super.onCreate(savedInstanceState); 11 setContentView(R.layout.activity_main); 12 } 13}
どうすれば良いのでしょうか? 教えていただけないでしょうか?
エラーが5つ出ているdebug/AndroidManifest.xmlです。(AndroidManifest.xmlにはエラーはでていません。)
java
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.android.sample.calculator" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="23" 9 android:targetSdkVersion="24" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@mipmap/ic_launcher" 14 android:label="@string/app_name" 15 android:supportsRtl="true" 16 android:theme="@style/AppTheme" > 17 <activity android:name="com.example.android.sample.calculator.MainActivity" > 18 <intent-filter> 19 <action android:name="android.intent.action.MAIN" /> 20 21 <category android:name="android.intent.category.LAUNCHER" /> 22 </intent-filter> 23 </activity> 24 </application> 25 26</manifest>
上のdebug/Manifest.mlxのエラー部分だけをカーソルを合わせた時に出てくるエラーとセットでまとめました。
java
1<manifest xmlns:android="http://schemas.android.com/apk/res/android" 2//ここには、URI is not registered(Settings|Project Settings|Schemas and DTDsがでます。 3 4 android:versionCode="1" 5//これには、Attribute android:versionCode is not allowed hereがでます。 6 7 android:versionName="1.0" > 8//これには、Attribute android:versionName is not allowed hereがでます。 9 10 android:allowBackup="true" 11//これには、Attribute android:allowBackup is not allowed hereがでます。 12 android:icon="@mipmap/ic_launcher" 13//これには、Attribute android:icon is not allowed hereがでます。 14 15 android:supportsRtl="true" 16//これには、Attribute android:supportsRtl is not allowed hereがでます。 17 18 android:theme="@style/AppTheme" > 19//これには、Attribute android:theme is not allowed hereがでます。
MainActivityです。
java
1package com.example.android.sample.calculator; 2 3import android.content.Intent; 4import android.support.v7.app.AppCompatActivity; 5import android.os.Bundle; 6import android.text.Editable; 7import android.text.TextUtils; 8import android.text.TextWatcher; 9import android.view.View; 10import android.widget.EditText; 11import android.widget.Spinner; 12import android.widget.TextView; 13 14public class MainActivity extends AppCompatActivity implements TextWatcher, View.OnClickListener{ 15 16 //上の計算ボタンを押した時のリクエストコード 17 private static final int REQUEST_CODE_ANOTHER_CALC_1=1; 18 //上の計算ボタンを押した時のリクエストコード 19 private static final int REQUEST_CODE_ANOTHER_CALC_2=2; 20 21 22 23 //上のEditText 24 private EditText numberInput1; 25 //下のEditText 26 private EditText numberInput2; 27 28 29 //演算子選択用のSpinner 30 private Spinner operatorSelector; 31 32 //計算結果表示用のTextView 33 private TextView calcResult; 34 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.activity_main); 39 40 41 //上のEditText 42 numberInput1=(EditText)findViewById(R.id.numberInput); 43 //上のEditTextの文字入力イベントを受け取る 44 numberInput1.addTextChangedListener(this); 45 46 //下のEditText 47 numberInput2=(EditText)findViewById(R.id.numberInput2); 48 49 //下のEditTextの文字入力を受け取る 50 numberInput2.addTextChangedListener(this); 51 52 53 54 55 //演算子選択用のSpinner 56 operatorSelector=(Spinner)findViewById(R.id.operatorSelector); 57 58 //計算結果表示用のTextView 59 calcResult=(TextView)findViewById(R.id.calcResult); 60 61 62 //上の計算ボタン 63 findViewById(R.id.calcButton1).setOnClickListener(this); 64 //下の計算ボタン 65 findViewById(R.id.calcButton2).setOnClickListener(this); 66 //続けて計算するボタン 67 findViewById(R.id.nextoButton).setOnClickListener(this); 68 69 } 70 71 72 @Override 73 public void onClick(View v){ 74 //タップされた時の処理を実装する 75 int id=v.getId(); 76 77 //IDごとに違う 78 switch(id){ 79 case R.id.calcButton1: 80 //上の計算ボタンかが押された時の処理 81 82 Intent intent1=new Intent(this,AnotherCalcActivity.class); 83 startActivityForResult(intent1,REQUEST_CODE_ANOTHER_CALC_1); 84 break; 85 case R.id.calcButton2: 86 //下の計算ボタンが押された時の処理 87 88 Intent intent2=new Intent(this,AnotherCalcActivity.class); 89 startActivityForResult(intent2,REQUEST_CODE_ANOTHER_CALC_2); 90 break; 91 case R.id.nextButton: 92 //続けて計算するボタンが押された時の処理 93 //両方のEditTextに値が設定されていれば、処理を行う 94 if(checkEditTextInput()){ 95 //計算する 96 int result=calc(); 97 //上のEditTextの値を書き換える 98 numberInput1.setText(String.valueOf(result)); 99 //計算し直して、画面を更新する 100 refreshResult(); 101 } 102 103 break; 104 } 105 106 107 } 108 109 @Override 110 protected void onActivityResult(int requestCode,int resultCode,Intent data){ 111 //startActivityResult()から戻ってきた時に呼ばれる 112 super.onActivityResult(requestCode,resultCode,data); 113 114 //結果がOKではない場合は何もしない 115 if(resultCode !=RESULT_OK)return; 116 117 //結果データセットを取り出す 118 Bundle resultBundle=data.getExtras(); 119 120 //結果データセットに、所定のキーが含まれていない場合、何もしない 121 if(!resultBundle.containsKey("result"))return; 122 123 //結果データから、"result"キーに対応するint値を取り出す。 124 int result=resultBundle.getInt("result"); 125 126 if(requestCode == REQUEST_CODE_ANOTHER_CALC_1) { 127 //上の計算ボタンを押した後戻ってきた場合 128 numberInput1.setText(String.valueOf(result)); 129 130 }else if(requestCode==REQUEST_CODE_ANOTHER_CALC_2){ 131 //下の計算ボタンを押した後戻ってきた場合 132 numberInput2.setText(String.valueOf(result)); 133 } 134 135 //計算をしなおして、結果を表示する 136 refreshResult(); 137 } 138 139 140 //2つのEditTextに入力がされているかをチェックする 141 private boolean checkEditTextInput(){ 142 //入力内容を取得する 143 String input1=numberInput1.getText().toString(); 144 String input2=numberInput2.getText().toString(); 145 146 //2つともから文字列(あるいはnull)でなければ、true 147 return !TextUtils.isEmpty(input1)&& !TextUtils.isEmpty(input2); 148 } 149 150 @Override 151 public void beforeTextChanged(CharSequence s,int start,int count,int after){ 152 //テキストが変更される直前に呼ばれる。Sは変更前の内容 153 154 } 155 156 @Override 157 public void onTextChanged(CharSequence s,int start,int before,int count){ 158 //テキストが変更される時に呼ばれる。sは変更後の内容で編集不可 159 } 160 161 @Override 162 public void afterTextChanged(Editable s){ 163 //テキストが変更された後に呼ばれる。sは変更後の内容で編集可能 164 //必要があれば、計算を行い、結果を表示する 165 refreshResult(); 166 167 } 168 169 //計算結果の表示を更新する 170 private void refreshResult(){ 171 if(checkEditTextInput()) { 172 //計算を行う 173 int result = calc(); 174 175 //計算結果用のTextVieを書き換える 176 String resultText = getString(R.string.calc_result_text, result); 177 calcResult.setText(resultText); 178 179 }else{ 180 //どちらかが入力されていない状態の場合、計算結果用の表示をデフォルトに戻す 181 calcResult.setText(R.string.calc_result_default); 182 } 183 } 184 185 186 //計算を行う 187 private int calc(){ 188 //入力内容を取得する 189 String input1=numberInput1.getText().toString(); 190 String input2=numberInput2.getText().toString(); 191 192 //int型に変換する 193 int number1=Integer.parseInt(input1); 194 int number2=Integer.parseInt(input2); 195 196 //Spinnerから、洗濯中のindexを取得する 197 int operator=operatorSelector.getSelectedItemPosition(); 198 199 //indexに応じて計算結果を返す 200 switch(operator){ 201 case 0://足し算 202 return number1+number2; 203 case 1://引き算 204 return number1-number2; 205 case 2://掛け算 206 return number1*number2; 207 case 3://割り算 208 return number1/number2; 209 default: 210 //通常発生しない 211 throw new RuntimeException(); 212 } 213 } 214 215 216 217 218} 219
どうぞよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/02 04:14