0

Im trying to learn how to use android studio, im not unfamiliar with java though. I tried making a boolean and if it is true, a text changes to "ON", otherwise it will change to "OFF" My app just crashes when im using USB-Debugging though.

Code: MainActivity.java

    package com.leuchtstein.flashlight;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
public boolean stat = false;
TextView text = (TextView) findViewById(R.id.textView)   ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


 public void triggerlight(View view) {

 if (stat == false) {


    text.setText("ON");
    stat = true;


} else {


    text.setText("OFF");
    stat = false;


  }



  }


 }

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:layout_height="match_parent"
  tools:context="com.leuchtstein.flashlight.MainActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="@string/desc_textview"
          android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
    app:layout_constraintHorizontal_bias="0.95"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="triggerlight"
    android:text="Turn on/off"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    app:layout_constraintVertical_bias="0.387" />

Well, thanks in advance.

3 Answers3

2

Don't call

TextView text = (TextView) findViewById(R.id.textView)   ;

in initialisation, rather call it in onCreate() method.

This is because Android first initializes the script, and calls onCreate only when setContentView() has been called and it has set up the button, &c. Thus findViewById(int id) can't find anything yet.

Put

text = (TextView) findViewById(R.id.textView);

in onCreate() and put

TextView text;

in initialisation instead.

  • It also might be good to mention that `findViewById` needs to be called *after* the call to `setContentView`. – fpsulli3 Jul 07 '17 at 19:40
  • Great! I made this mistake too when I learnt it :) If it's helpful, click the 'accept answer' button on the left :) –  Jul 08 '17 at 13:49
0

Try this :

public class MainActivity extends AppCompatActivity {
    public boolean stat = false;
    TextView text;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.textView)   
}
Karim
  • 312
  • 1
  • 10
0

You haven't provided the logcat or any other any ifo, but one thing I can see here that could go wrong is this :

TextView text = (TextView) findViewById(R.id.textView);

Change your code to look like this :

public class MainActivity extends AppCompatActivity {

TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    text = (TextView) findViewById(R.id.textView)   
}

So the difference is, you assign a value to your TextView using findViewById, after you have have called setContentView(). All view assignments should be only done after setContentView()is called in activities because that is where you set your root view and when you call findViewById(), the system looks up a view with an id in the root view.

rgv
  • 1,072
  • 1
  • 15
  • 36