2017-09-17 16 views
0

TextViewを別のクラスから更新しようとしていますが、クラッシュしています。別のクラスからTextViewを更新するにはどうすればよいですか?

「アプリがロックされています」と言われており、パスコードを入力する次のレイアウトに移動します。
ユーザーが正しいパスワードを入力すると、アプリはユーザーに前のテキストビューに移動し、「現在アプリがロックされていません」と変更されました。

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

を試すことができますテキストが非常に長い....あなたのアプリがクラッシュ*した場合、あなたのquesitonにlogcatを追加する必要があります –

答えて

0
  1. すべての活動は、マニフェストにあることを確認してください。

  2. すべてのXML要素が正しいレイアウトになっていることを確認してください。たとえば、android:id="@+id/text"activity_second.xmlの一部ではないため、nullです。一般的に


、あなたは間違って周りのテントオブジェクトを渡しています。あなたはサイクルがあります。

MainActivityがメイン

Intent intent = new Intent(SecondActivity.this, 
MainActivity.class); 
startActivity(intent); 

を開始し、あなたがスタックにこの方法をアクティビティのインスタンスを追加し続けることができ、その後第二

private void confirm(String s){ 
    Intent intent = new Intent(MainActivity.this, SecondActivity.class); 
    startActivity(intent); 
} 

セカンドを開始します。あなたがする必要がどのような

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); 
} 
関連する問題