繁体   English   中英

Android-TextView上的NullPointerException

[英]Android - NullPointerException on a TextView

我遇到无法解决的问题。 我要声明并初始化一些TextView。 但是,当我将其背景设置为彩色时,我有一个NullPointerException。

这是必要的代码:

public static ArrayList<Integer> questList = new ArrayList<Integer>();
public static ArrayList<TextView> scores = new ArrayList<TextView>();
public static int quest = 50, scoreIndex = 0;
public boolean first = true, getIndex = false;
public int corrects = 0, incorrects = 0, index = 0, indexCons = 0;
public int correctButton = 0;
public Button ans1, ans2, ans3, ans4;
LinearLayout ln_an1, ln_an2, ln_an3, ln_an4;
public TextView question;
public TextView s1, s2, s3, s4, s5, s6, s7, s8, s9, s10;
public TextView correctsText, correctsNum, incorrectsText, incorrectsNum;
public String currentQuest;
public Handler handler = new Handler();

@Override
public void onBackPressed() {}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getActionBar().hide();
    setContentView(R.layout.container_layout);


    if(savedInstanceState != null){}

    scoreIndex = 0;
    QES.createQuestions();
    AES.createAnswers();


    s1 = (TextView) findViewById(R.id.score1);
    s2 = (TextView) findViewById(R.id.score2);
    s3 = (TextView) findViewById(R.id.score3);
    s4 = (TextView) findViewById(R.id.score4);
    s5 = (TextView) findViewById(R.id.score5);
    s6 = (TextView) findViewById(R.id.score6);
    s7 = (TextView) findViewById(R.id.score7);
    s8 = (TextView) findViewById(R.id.score8);
    s9 = (TextView) findViewById(R.id.score9);
    s10 = (TextView) findViewById(R.id.score10);
    scores.add(s1);scores.add(s2);scores.add(s3);scores.add(s4);
    scores.add(s5);scores.add(s6);scores.add(s7);scores.add(s8);
    scores.add(s9);scores.add(s10);

    s1.setBackgroundColor(Color.BLACK);
    s2.setBackgroundColor(Color.BLACK);
    s3.setBackgroundColor(Color.BLACK);
    s4.setBackgroundColor(Color.BLACK);
    s5.setBackgroundColor(Color.BLACK);
    s6.setBackgroundColor(Color.BLACK);
    s7.setBackgroundColor(Color.BLACK);
    s8.setBackgroundColor(Color.BLACK);
    s9.setBackgroundColor(Color.BLACK);
    s10.setBackgroundColor(Color.BLACK);

    if(findViewById(R.id.fragment_container) != null){

        if(savedInstanceState != null){
            return;
        }

        TenQuestions tq = new TenQuestions();

        tq.setArguments(getIntent().getExtras());

        getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, tq).commit();

    }`

我在“ s1.setBackgroundColor(Color.BLACK);”行有异常

编辑:这是container_layout.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"
tools:context="com.panagetstudio.quiz.TenQuestionsGameActivity" >

</FrameLayout>

这是带有textviews的布局的一部分:

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:background="@color/white">

        <TextView android:id="@+id/score1"
            android:layout_width="match_parent"
            android:textSize="12sp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"/>

    </LinearLayout>

(重复10次)

尝试通过调用rootView.findViewById(R.id.your_tv_id)初始化FragmentTextViews

例如,在您的Fragment

public static class YourFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.example_fragment, container, false);

        TextView s1 = (TextView) rootView.findViewById(R.id.score1);

        s1.setBackgroundColor(Color.BLACK);
    }
}

它现在返回null因为这些TextView对象不在您的Activity而不是Fragment container_layout.xml

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM