-6

After much research, I have been unable to figure out how I can initialize my TextView in my java file in Android Studio. The TextView in question is located in a different layout file so I don't know the correct syntax to use. I think my question is similar to: Null pointer Exception on .setOnClickListener But the solution for him is not working for me. Here is my troublesome code:

 Microsoft = (Button) findViewById(R.id.Microsoft);
        Microsoft.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

                //TextView score = (TextView) findViewById(R.id.score);
                TextView score = (TextView) 
score.findViewById(R.id.question2);
                (score).setText(0);

            }
        });

TheFindViewById part is the part I need.

FULL CODE VVVV

package org.flinthill.finalprojectv3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    Button SuSe;
    Button DOS;
    Button B;
    Button BIOS;
    Button Microsoft;
    Button LenBosackandSandyLerner;
    Button HaskelDiklah;
    Button SteveWozniak;

    SuSe = (Button) findViewById(R.id.SuSe);
    SuSe.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText(0);

        }
    });
    DOS = (Button) findViewById(R.id.DOS);
    DOS.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText("0");

        }
    });

    B = (Button) findViewById(R.id.B);
    B.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText("0");

        }
    });
    BIOS = (Button) findViewById(R.id.BIOS);
    BIOS.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Right!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText("1");

            setContentView(R.layout.question2);

        }
    });

    //QUESTION 2

    Microsoft = (Button) findViewById(R.id.Microsoft);
    Microsoft.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

            //TextView score = (TextView) findViewById(R.id.score);
            TextView score = (TextView) 
score.findViewById(R.id.question2);
            (score).setText(0);

        }
    });
    /*LenBosackandSandyLerner = (Button) 
findViewById(R.id.LenBosackandSandyLerner);
    LenBosackandSandyLerner.setOnClickListener(new 
View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Right!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText("2");

            setContentView(R.layout.question3);

        }
    });

    HaskelDiklah = (Button) findViewById(R.id.HaskelDiklah);
    HaskelDiklah.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText("0");

        }
    });
    SteveWozniak = (Button) findViewById(R.id.SteveWozniak);
    SteveWozniak.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(getApplicationContext(), "Wrong!", 
Toast.LENGTH_SHORT).show();

            TextView score = (TextView) findViewById(R.id.score);
            (score).setText("0");

        }
    });*/

}

}

XML CODE:

LAYOUT 1:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:id="@+id/LUL"
    android:textColor="@color/LightGreen"
    android:layout_height="wrap_content"
    android:typeface="serif"
    android:text="Which is NOT an OS?"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:textSize="24sp"/>

<Button
    android:id="@+id/SuSe"
    android:onClick="SuSeClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SuSe"
    android:layout_marginTop="100dp"
    android:layout_below="@+id/LUL"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/BIOS"
    android:onClick="BIOSClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BIOS"
    android:layout_below="@+id/SuSe"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/DOS"
    android:onClick="DOSClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="DOS"
    android:layout_below="@+id/BIOS"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/B"
    android:onClick="BClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B"
    android:layout_below="@+id/DOS"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/score"
    android:textColor="@color/colorAccent"
    android:text="0"
    android:textSize="32dp"
    android:layout_below="@+id/LUL"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp" />
</RelativeLayout>

LAYOUT 2:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/question2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:id="@+id/question2text"
    android:textColor="@color/LightGreen"
    android:layout_height="wrap_content"
    android:typeface="serif"
    android:text="Who created Cisco"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="32dp"
    android:textSize="24sp"/>

<Button
    android:id="@+id/Microsoft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Microsoft"
    android:layout_marginTop="100dp"
    android:layout_below="@+id/question2text"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/LenBosackandSandyLerner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Len Bosack and Sandy Lerner"
    android:layout_below="@+id/Microsoft"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/HaskelDiklah"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Haskel Diklah"
    android:layout_below="@+id/LenBosackandSandyLerner"
    android:layout_centerHorizontal="true" />

<Button
    android:id="@+id/SteveWozniak"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Steve Wozniak"
    android:layout_below="@+id/HaskelDiklah"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/score"
    android:textColor="@color/colorAccent"
    android:text="1"
    android:textSize="32dp"
    android:layout_below="@+id/question2text"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp" />
</RelativeLayout>
Community
  • 1
  • 1
KamehK
  • 3
  • 2

2 Answers2

0

You cannot do this unless your TextView is within the layout your Activity, Fragment or Dialog controls.

The findViewById method looks for Views within the layout previously configured by the setContentView(layout) or the layout inflated in your Fragment or Dialog. If it does not find anything, your TextView will have a null reference.

Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42
0
   //Change the name id in the xml file
   Button microsoftButton = (Button) findViewById(R.id.microsoftButton);
   TextView scoreTextView = (TextView) findViewById(R.id.scoreTextView);

       microsoftButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(getApplicationContext(),"Wrong!", Toast.LENGTH_SHORT).show();

               // This was declared above so now you can use it. You can only set it to a String not to an Integer.
               scoreTextView.setText("0");

          }
       });
fsebek
  • 389
  • 2
  • 9
  • You have three options. 1. You inflate it using the inflator service. 2. You create a custom button with extends the standard Button class. 3. You can include external xml files into the activities xml by using – fsebek May 22 '17 at 12:44
  • I will try inflating it. Thanks. – KamehK May 22 '17 at 14:33
  • Remember that after inflating you need to add it as well! – fsebek May 22 '17 at 14:55