-1

I created 3 new projects in my android studio 3.1.3. The problem is each time I add setonclick to a button or manually add any view the application crashes, this is one of the programs I created THIS IS MY MAIN.JAVA FILE

public class MainActivity extends AppCompatActivity {

Button btnRun, btnClear;
EditText txtInput, txtOutput;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    txtInput   = findViewById(R.id.txtInput);
    txtOutput  = findViewById(R.id.txtOutput);
    btnClear   = findViewById(R.id.btnClear);
    btnRun     = findViewById(R.id.btnRun);
    setContentView(R.layout.activity_main);

    /*btnRun.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "Working", Toast.LENGTH_SHORT).show();
        }
    });*/


    txtInput.setText("2");
    txtOutput.setText("2 x 1 = 2\n" +
            "2 x 2 = 4");

THIS IS MY ACTIVITY.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:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
    android:id="@+id/txtOutput"
    android:layout_width="221dp"
    android:layout_height="0dp"
    android:layout_marginBottom="18dp"
    android:ems="10"
    android:inputType="textMultiLine"
    app:layout_constraintBottom_toTopOf="@+id/btnClear"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtInput" />

<Button
    android:id="@+id/btnClear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="60dp"
    android:layout_marginEnd="29dp"
    android:text="@string/clear_button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/btnRun"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/txtOutput" />

<Button
    android:id="@+id/btnRun"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="9dp"
    android:layout_marginTop="18dp"
    android:text="@string/run_button"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/btnClear"
    app:layout_constraintTop_toBottomOf="@+id/txtOutput" />

<EditText
    android:id="@+id/txtInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="27dp"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/txtOutput"
    app:layout_constraintStart_toStartOf="@+id/txtOutput"
    app:layout_constraintTop_toTopOf="parent" />

Please help me find the problem But if I take or comment on the 'setOnClick' or take out the setText the program runs

1 Answers1

0

Try this

public class MainActivity extends AppCompatActivity {

Button btnRun, btnClear;
EditText txtInput, txtOutput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);  // here is the change
txtInput   = findViewById(R.id.txtInput);
txtOutput  = findViewById(R.id.txtOutput);
btnClear   = findViewById(R.id.btnClear);
btnRun     = findViewById(R.id.btnRun);


/*btnRun.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(MainActivity.this, "Working", Toast.LENGTH_SHORT).show();
    }
});*/


txtInput.setText("2");
txtOutput.setText("2 x 1 = 2\n" +
        "2 x 2 = 4");

When you put initialization before your setting view. it will gives null because all of your view in activity and you are setting it after initialization.

Shubham Vala
  • 1,024
  • 7
  • 18
  • 2
    Could you explain *how* this solves OP's issue? I know you've moved the `setContentView()` method, but it may not be instantly apparent for anyone else reading this answer in future. – Michael Dodd Jul 31 '18 at 13:05
  • 1
    Putting *"here is the change"* still doesn't explain **why** making this change solves this issue. All it takes is a little explanation like "You're trying to find your views before you've inflated the layout. Inflate the layout first" or words to that effect. It's an answer that works, but not an answer I'd upvote. – Michael Dodd Jul 31 '18 at 13:08