我正在开发一个Android应用程序,在我将textview添加到主要活动布局中之前,它一直工作良好。
正在显示所有图像。但是,与textview无关,而是在显示屏上显示了无关的内容。 下面是mainactivity.java中的代码。
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
ImageButton showBalDialog, showExpDialog, showIncomebtn, showExpbtn;
TextView bal_dialog, total_incomes;
public static final String DB_name = "mydb";
DatabaseHelper m_db;
private String amnt, src, exp, rzn;
private String incomes = "sum";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_db = new DatabaseHelper(this);
showBalDialog = findViewById(R.id.btnshowBalDialog);
showExpDialog = findViewById(R.id.btnshowExpDialog);
showIncomebtn = findViewById(R.id.btnshowIncomes);
showExpbtn = findViewById(R.id.btnshowExpenses);
total_incomes = findViewById(R.id.totalincomes);
incomes = String.valueOf(total_incomes);
total_incomes.setText(incomes);
}
}
下面是来自activity_main.xml的代码
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:screenOrientation="portrait"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/btnshowBalDialog"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/add_inc"> // this is the image(eye)
</ImageButton>
<ImageButton
android:id="@+id/btnshowExpDialog"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/add_exp"> // this is the image(eye)
</ImageButton>
<ImageButton
android:id="@+id/btnshowIncomes"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnshowBalDialog"
android:background="@drawable/income_his"> // this is the image(eye)
</ImageButton>
<ImageButton
android:id="@+id/btnshowExpenses"
android:scaleType="fitXY"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="40dp"
android:cropToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnshowExpDialog"
android:background="@drawable/expense_his"> // this is the image(eye)
</ImageButton>
<TextView
android:id="@+id/totalincomes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:text="TextView"
app:layout_constraintTop_toBottomOf="@id/btnshowIncomes"
app:layout_constraintStart_toStartOf="parent">
</TextView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:1)
total_incomes 是Textview,您将TextView与xml中存在的R.id.totalincome绑定。
incomes = String.valueOf(total_incomes);
total_incomes.setText(incomes);
您要获取 total_incomes 的字符串值,并将其存储在 incomes 中,并与在 total_incomes 中设置的值相同。简而言之,您要设置textview ID。
答案 1 :(得分:1)
您必须这样做:
total_incomes.setText(incomes);
您的收入值已经在字符串中,无需使用String.valueOf()
代替
incomes = String.valueOf(total_incomes);
total_incomes.setText(incomes);