我遇到了一个无法解决的问题。我正在声明并初始化一些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次)
答案 0 :(得分:0)
尝试通过调用TextViews
初始化Fragment
中的rootView.findViewById(R.id.your_tv_id)
。
例如,在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
对象不在您的container_layout.xml
中,而是Activity
,而不是Fragment
。