我试图从另一个类更新我的TextView,但它一直在崩溃。
最初说"应用已被锁定",并将用户带到下一个输入密码的布局。
当用户输入正确的密码时,应用程序会将用户转到上一个,并且textview已更改说“#34;应用程序现已解锁!"
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button)findViewById(R.id.btn);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
confirm(login.toString());
}
});
}
private void confirm(String s){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
第二项活动:
public class SecondActivity extends AppCompatActivity implements
View.OnClickListener {
public boolean first = false;
public boolean second = false;
public boolean third = false;
public boolean fourth = false;
public boolean submit = false;
public TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button first = (Button) findViewById(R.id.btn1);
first.setOnClickListener(this);
Button second = (Button) findViewById(R.id.btn2);
second.setOnClickListener(this);
Button third = (Button) findViewById(R.id.btn3);
third.setOnClickListener(this);
Button fourth = (Button) findViewById(R.id.btn4);
fourth.setOnClickListener(this);
Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(this);
textView = (TextView)findViewById(R.id.text);
}
public int counter;
public void onClick(View v) {
counter++;
switch (v.getId()) {
case R.id.btn1:
if (counter == 1) {
first = true;
}
break;
case R.id.btn2:
if (counter == 2) {
second = true;
}
break;
case R.id.btn3:
if (counter == 3) {
third = true;
}
break;
case R.id.btn4:
if (counter == 4) {
fourth = true;
}
break;
case R.id.submit:
submit = true;
open();
break;
}
}
public void open() {
if (first && second && third && fourth && submit) {
textView.setText("Your App is now Unlocked!");
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
}
MainActivity布局:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="unlock"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Welcome to the app! The app is LOCKED!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238" />
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
确保所有活动都在清单中。
确保所有XML元素都在正确的布局中。例如,android:id="@+id/text"
不是activity_second.xml
的一部分,因此它为空。
通常,您错误地传递Intent对象。你有一个周期。
MainActivity启动第二个
private void confirm(String s){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
然后开始Main
Intent intent = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(intent);
你可以继续以这种方式将Activity实例添加到堆栈中。
您需要做的是How to manage `startActivityForResult` on Android?
Getting a Result from an Activity。
首先,你可以试试这个
public static final int CONFIRM_RESULT = 1;
private void confirm(String s){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("passphrase", s);
startActivityForResult(intent, CONFIRM_RESULT);
}