如何从其他布局初始化变量?

时间:2017-05-22 12:22:27

标签: java android

经过大量研究,我无法弄清楚如何在Android Studio的java文件中初始化TextView。有问题的TextView位于不同的布局文件中,因此我不知道要使用的正确语法。我认为我的问题类似于:Null pointer Exception on .setOnClickListener 但他的解决方案并不适合我。 这是我麻烦的代码:

 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部分是我需要的部分。

完整代码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:

布局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>

布局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>

2 个答案:

答案 0 :(得分:0)

除非TextView位于ActivityFragmentDialog控件的布局范围内,否则无法执行此操作。

findViewById方法在先前由Views配置的布局或setContentView(layout)Fragment中夸大的布局中查找Dialog。如果找不到任何内容,则TextView将有一个空引用。

答案 1 :(得分:0)

yvalue = c(.1, .2, .3, .2, .1, .2, .3, .1)
df = data.frame(yvalue)
df$name = c("a", "b", "c", "d", "e", "f", "g", "h")
df$type = c("apple", "apple", "apple", "apple", "apple", "banana", "banana", "banana")
ggplot(data = df) + geom_bar(aes(y = yvalue, x=type, fill=name), stat = "identity", position = position_dodge())