Java Android-文本视图上的NullPointerException
我有个无法解决的问题。我正在声明和初始化一些文本视图。但是,当我把它们的背景颜色,我有一个NullPointerException 以下是必要的代码:Java Android-文本视图上的NullPointerException,java,android,nullpointerexception,textview,Java,Android,Nullpointerexception,Textview,我有个无法解决的问题。我正在声明和初始化一些文本视图。但是,当我把它们的背景颜色,我有一个NullPointerException 以下是必要的代码: public static ArrayList<Integer> questList = new ArrayList<Integer>(); public static ArrayList<TextView> scores = new ArrayList<TextView>(); public st
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();
}`
下面是包含文本视图的部分布局:
<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\u tv\u id)
初始化片段中的文本视图
例如,在您的片段中
:
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\u layout.xml
中,它是用于您的活动的,而不是您的Fragment发布您的container\u layout.xml
。您可以发布容器布局吗?清理并构建您的项目,另外,请确保您在set content view中使用的布局应有一个id为score1的文本视图,并发布您的xml,如果它不能解决您的问题,则无法正常工作,这是container_layout.xml我刚刚试用过,这似乎不是解决方案。您可以发布您的片段
的外观以及您的片段
的相应布局是什么吗?我的片段只有setContentView()方法,仅此而已,我的片段的相应布局是我在回答中编辑的代码部分。它以前是这样工作的,但我不知道为什么它会抛出一个异常,我很沮丧。您必须在Fragment类中实现onCreateView
。请参阅我的编辑以获取示例。我已经实现了它,当我说“setContentView()”
时,我的意思是“onCreateView()”
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);
}
}