このコードのR.の部分が、コンパイルエラーとなってしまいます。
java
1package jp.study.chatapplication; 2import android.os.Bundle; 3import android.support.design.widget.FloatingActionButton; 4import android.support.design.widget.Snackbar; 5import android.support.v7.app.ActionBarActivity; 6import android.support.v7.app.AppCompatActivity; 7import android.support.v7.widget.Toolbar; 8import android.view.Gravity; 9import android.view.View; 10import android.view.Menu; 11import android.view.MenuItem; 12import android.view.animation.Animation; 13import android.view.animation.TranslateAnimation; 14import android.widget.Button; 15import android.widget.EditText; 16import android.widget.LinearLayout; 17import android.widget.TextView; 18import android.widget.Toast; 19import java.util.Calendar; 20 21 public class ChatActivity extends AppCompatActivity implements View.OnClickListener{ 22 23 private EditText mInputMessage; 24 private Button mSendMessage; 25 private LinearLayout mMessageLog; 26 private TextView mCpuMessage; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_chat); 32 //XMLのViewを取得します。 33 mInputMessage=(EditText)findViewById(R.id.input_message);//ユーザーが入力するフィールド 34 mSendMessage=(Button)findViewById(R.id.send_message);//SENDボタン 35 mMessageLog=(LinearLayout)findViewById(R.id.message_log);//入力履歴を表示するレイアウト 36 mCpuMessage=(TextView)findViewById(R.id.cpu_message);//コンピューターからの応答 37 mSendMessage.setOnClickListener(this); 38 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 39 setSupportActionBar(toolbar); 40 } 41 @Override 42 public boolean onCreateOptionsMenu(Menu menu) { 43 // Inflate the menu; this adds items to the action bar if it is present. 44 getMenuInflater().inflate(R.menu.menu_chat, menu); 45 return true; 46 } 47 @Override 48 public boolean onOptionsItemSelected(MenuItem item) { 49 // Handle action bar item clicks here. The action bar will 50 // automatically handle clicks on the Home/Up button, so long 51 // as you specify a parent activity in AndroidManifest.xml. 52 int id = item.getItemId(); 53 //noinspection SimplifiableIfStatement 54 if (id == R.id.action_settings) { 55 return true; 56 } 57 return super.onOptionsItemSelected(item); 58 } 59 @Override 60 //ここに追加する 61 public void onClick(View v) { 62 //このメソッドalt enterで作られた 63 if(v.equals(mSendMessage)){ 64 //SENDボタンが押された時の処理 65 String inputText=mInputMessage.getText().toString(); 66 String answer; 67 TextView userMessage=new TextView(this);//新しいTextViewを作る 68 userMessage.setBackgroundResource(R.drawable.user_message); 69 userMessage.setText(inputText);//TextViewに入力したテキストをセットする 70 71 //コメントサイズに合わせる 72 LinearLayout.LayoutParams userMessageLayout=new LinearLayout.LayoutParams( 73 LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 74 userMessageLayout.gravity=Gravity.END; 75 76 //端末によって最適な隙間をつける 77 final int marginSize=getResources().getDimensionPixelSize(R.dimen.message_margin); 78 79 //隙間を設定する 80 userMessageLayout.setMargins(0, marginSize, 0, marginSize); 81 mMessageLog.addView(userMessage,0,userMessageLayout);//TextViewをViewの一番上にセット 82 83 if(inputText.contains("元気ですか")) { 84 answer = "元気です"; 85 }else if(inputText.contains("疲れた")) { 86 answer = "お疲れ様です"; 87 }else if(inputText.contains("おみくじ")) { 88 double random = Math.random() * 5.1d; 89 if (random < 1d) { 90 answer = "大凶"; 91 } else if (random < 2d) { 92 answer = "凶"; 93 } else if (random < 3d) { 94 answer = "吉"; 95 } else if (random < 4d) { 96 answer = "中吉"; 97 } else if (random < 5d) { 98 answer = "大吉"; 99 } else { 100 answer = "超大吉"; 101 } 102 }else if(inputText.contains("時間")){ 103 Calendar cal= Calendar.getInstance(); 104 int hour=cal.get(Calendar.HOUR);//時 105 int minute=cal.get(Calendar.MINUTE);//分 106 int second=cal.get(Calendar.SECOND);//秒 107 answer=String.format("ただいまの時間は%1$d時%2$d分%3$d秒です", hour, minute, second); 108 109 }else { 110 answer = "それはいいですね"; 111 } 112 113 final TextView cpuMessage=new TextView(this);//内部クラスから参照するためfinalで宣言 114 115 cpuMessage.setBackgroundResource(R.drawable.cpu_message); 116 117 cpuMessage.setText(answer); 118 cpuMessage.setGravity(Gravity.START); 119 mInputMessage.setText(""); 120 TranslateAnimation userMessageAnimation=new TranslateAnimation(mMessageLog.getWidth(),0,0,0); 121 userMessageAnimation.setDuration(1000); 122 userMessageAnimation.setAnimationListener(new Animation.AnimationListener() { 123 //クラス名が宣言されていない内部クラスを無名内部クラスと言います。 124 @Override 125 public void onAnimationStart(Animation animation) { 126 } 127 @Override 128 public void onAnimationEnd(Animation animation) { 129 //コメントサイズに合わせる 130 LinearLayout.LayoutParams cpuMessageLayout=new LinearLayout.LayoutParams( 131 LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); 132 cpuMessageLayout.gravity=Gravity.START; 133 //隙間を設定する 134 135 cpuMessageLayout.setMargins(marginSize,marginSize,marginSize,marginSize); 136 mMessageLog.addView(cpuMessage,0,cpuMessageLayout); 137 138 TranslateAnimation cpuAnimation=new TranslateAnimation(-mMessageLog.getWidth(),0,0,0); 139 cpuAnimation.setDuration(1000); 140 cpuMessage.setAnimation(cpuAnimation); 141 } 142 143 @Override 144 public void onAnimationRepeat(Animation animation) { 145 } 146 }); 147 userMessage.startAnimation(userMessageAnimation); 148 } 149 } 150} 151
エラーこのようになります。
java
1Error:(22, 68) No resource found that matches the given name (at 'layout_margin' with value '@dimen/fab_margin').Error:(22, 68) No resource found that matches the given name (at 'layout_margin' with value '@dimen/fab_margin'). 2Error:Execution failed for task ':chatapplication:processDebugResources'. 3> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Users/taichi/Library/Android/sdk/build-tools/23.0.0/aapt'' finished with non-zero exit value 1
一方Activity_chat.xmlでは、
java
1android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
ここの@dimen/fab_marginにコンパイルエラーがあります。
ぐぐってみた所cleanbuildをしてくださいと書いてあったのですが、ダメでした。わたし的には、xmlでの指定がなされていないのかと思って、dimens.xmlに
<dimen name="fab_margin">4dp</dimen>を足したのですが、
java
1<resources> 2 <!-- Default screen margins, per the Android Design guidelines. --> 3 <dimen name="activity_horizontal_margin">16dp</dimen> 4 <dimen name="activity_vertical_margin">16dp</dimen> 5 <dimen name="massage_margin">4dp</dimen> 6 <dimen name="fab_margin">4dp</dimen> 7</resources>
となり、エラーは
java
1Error:(99, 78) エラー: シンボルを見つけられません 2シンボル: 変数 message_margin 3場所: クラス dimen
となります。R.のコンパイルエラーは消えるのですが、名前の指定は間違えていないのですが、なぜなのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/20 07:01
2016/05/20 07:07
2016/05/20 07:14
2016/05/20 08:17 編集
2016/05/20 08:27
2016/05/20 08:35 編集
2016/05/20 08:34
2016/05/20 08:37