مشکل در طراحی و برنامه نویسی پروژه Calculator (سری آموزش های جدید)
سلام دوست عزیز
بهتر بود بر روی فایل R که خطا میداد نگه میداشتین و از ارورش عکس میذاشتین که خطا مربوطه قابل تشخیص باشه ;)
معمولا خطای فایل R برای شناخته نشدن اون هست، از منوی Build ابتدا Clean project و بعد Rebuild کنین احتمالا باید درست بشه...
اگه جواب نداد میتونین کدهای برنامه رو کپی کنین و پروژه جدیدی ایجاد کنین.
سلام
هر چی Import R داخل پروژه داری (داخل کلاس ها در قسمت بالا) حذف کن بعدش کامپایل رو بزن ایکونی شبیه به چکش سبز رنگ بالا هستش
مشکل Import R معمولا موقعی پیش میاد که مثلا عکس یا فایلی داخل res داشته باشی که با حروف بزرگ نوشته شده باشه و از این جور مشکلات
سلام اینم از متن ایکس ام ال دوست عزیز
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sina.afkhamzadeh.calculator.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="vertical">
<TextView
android:id="@+id/txt_history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:textColor="#555555"
android:textSize="18dp"/>
<TextView
android:id="@+id/txt_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|right"
android:text="0"
android:textColor="#000000"
android:textSize="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:orientation="horizontal">
<Button
android:id="@+id/btn_clearEntity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="CE"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="C"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_back"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="<"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_division"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="/"
android:textColor="#000000"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:orientation="horizontal">
<Button
android:id="@+id/btn_7"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="7"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_8"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="8"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_9"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="9"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_multiply"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="X"
android:textColor="#000000"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:orientation="horizontal">
<Button
android:id="@+id/btn_4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="4"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="5"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_6"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="6"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_minus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="-"
android:textColor="#000000"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:orientation="horizontal">
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="1"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="2"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="3"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_plus"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="+"
android:textColor="#000000"
android:textSize="22dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.18"
android:orientation="horizontal">
<Button
android:id="@+id/btn_sign"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="+-"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="0"
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_dot"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="."
android:textColor="#000000"
android:textSize="22dp"/>
<Button
android:id="@+id/btn_equal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:width="0dp"
android:text="="
android:textColor="#000000"
android:textSize="22dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
اینم از متن MainActivity.java :
package sina.afkhamzadeh.calculator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_dot;
Button btn_sign;
Button btn_back;
Button btn_clear;
Button btn_clearEntity;
Button btn_division;
Button btn_multiply;
Button btn_minus;
Button btn_plus;
Button btn_equal;
TextView txt_history;
TextView txt_result;
boolean mustReset = false;
float currentResult = 0;
String operand = "";
public void toggleSign(){
String resultValue = txt_result.getText().toString();
if (resultValue.contains("-")){
resultValue = resultValue.replace("-" , "");
}else{
resultValue = "-" + resultValue;
}
txt_result.setText(resultValue);
}
public void compute(String nextOperand) {
String resultValue = txt_result.getText().toString();
float resultNumber = Float.parseFloat(resultValue);
if (operand.equals("+")) {
currentResult += resultNumber;
} else if (operand.equals("-")) {
currentResult -= resultNumber;
} else if (operand.equals("*")) {
currentResult *= resultNumber;
} else if (operand.equals("/")) {
currentResult /= resultNumber;
} else if (operand.equals("")) {
currentResult = resultNumber;
}
String oldHistory = txt_history.getTag().toString();
txt_history.setText(oldHistory + " " + resultNumber + " " + nextOperand);
txt_result.setText("" + currentResult);
operand = nextOperand;
mustReset = true;
}
public void processEqual() {
compute("");
txt_history.setText("");
}
public void doOperand(String nextOperand) {
compute(nextOperand);
}
public void backOneLetter() {
String value = txt_result.getText().toString();
String newValue = value.substring(0, value.length() - 1);
txt_result.setText(newValue);
}
public void addPoint() {
if (mustReset) {
txt_result.setText("0");
mustReset = false;
}
String oldValue = txt_result.getText().toString();
if (oldValue.contains(".")) {
return;
}
if (oldValue.length() > 9) {
return;
}
txt_result.setText(oldValue + ".");
}
public void appendNumber(int num) {
if (mustReset) {
txt_result.setText("");
mustReset = false;
}
String oldValue = txt_result.getText().toString();
if (oldValue.length() > 9) {
return;
}
if (oldValue.equals("0")) {
if (num == 0) {
return;
} else {
oldValue = "";
}
}
txt_result.setText(oldValue + num);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0 = (Button)findViewById(R.id.btn_0);
btn_1 = (Button)findViewById(R.id.btn_1);
btn_2 = (Button)findViewById(R.id.btn_2);
btn_3 = (Button)findViewById(R.id.btn_3);
btn_4 = (Button)findViewById(R.id.btn_4);
btn_5 = (Button)findViewById(R.id.btn_5);
btn_6 = (Button)findViewById(R.id.btn_6);
btn_7 = (Button)findViewById(R.id.btn_7);
btn_8 = (Button)findViewById(R.id.btn_8);
btn_9 = (Button)findViewById(R.id.btn_9);
btn_dot = (Button)findViewById(R.id.btn_dot);
btn_sign = (Button)findViewById(R.id.btn_sign);
btn_back = (Button)findViewById(R.id.btn_back);
btn_clear = (Button)findViewById(R.id.btn_clear);
btn_clearEntity = (Button)findViewById(R.id.btn_clearEntity);
btn_division = (Button)findViewById(R.id.btn_division);
btn_multiply = (Button)findViewById(R.id.btn_multiply);
btn_minus = (Button)findViewById(R.id.btn_minus);
btn_plus = (Button)findViewById(R.id.btn_plus);
btn_equal = (Button)findViewById(R.id.btn_equal);
txt_history = (TextView)findViewById(R.id.txt_history);
txt_result = (TextView)findViewById(R.id.txt_result);
btn_clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt_result.setText("0");
txt_history.setText("");
currentResult = 0;
mustReset = false;
}
});
btn_clearEntity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt_result.setText("0");
}
});
View.OnClickListener numberListener = new View.OnClickListener(){
@Override
public void onClick(View view){
int index = Integer.parseInt(view.getTag().toString());
appendNumber(index);
}
};
btn_0.setOnClickListener(numberListener);
btn_1.setOnClickListener(numberListener);
btn_2.setOnClickListener(numberListener);
btn_3.setOnClickListener(numberListener);
btn_4.setOnClickListener(numberListener);
btn_5.setOnClickListener(numberListener);
btn_6.setOnClickListener(numberListener);
btn_7.setOnClickListener(numberListener);
btn_8.setOnClickListener(numberListener);
btn_9.setOnClickListener(numberListener);
View.OnClickListener operandListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
doOperand(view.getTag().toString());
}
};
btn_division.setOnClickListener(operandListener);
btn_multiply.setOnClickListener(operandListener);
btn_minus.setOnClickListener(operandListener);
btn_plus.setOnClickListener(operandListener);
btn_equal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
processEqual();
}
});
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
backOneLetter();
}
});
btn_dot.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addPoint();
}
});
btn_sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleSign();
}
});
}
}
پیدا کردن مشکلش که خیلی راحت بود زمانی که زدم اجرا خود اندروید استودیو منو به خطی از کد شما که اشکال داشت هدایت کرد و من با اصلاح اون خط تونستم مشکل رو حل کنم. حالا شاید دلیل اینکه خود اندروید استودیو منو هدایت کرد به خط ارور از فایل xml، این باشه که من از نسخه 3 اندروید استودیو استفاده می کنم و پیشنهاد میکنم شما هم آپدیت کنید.
و اما مشکل از این خطه:
android:text="<"
که کاراکتر < به عنوان متن تنظیم شده
پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .












