为什么我无法在动态创建的ImageButton中更改图像

时间:2017-01-15 11:31:55

标签: java android

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_teacherseatcheck);



    Button btn1 =(Button)findViewById(R.id.button);
    btn1.setId(a);
    btn1.performClick();

    Intent it= getIntent();
    String studentid = it.getStringExtra("Studentseated");

    String[] AfterSplit = studentid.split(",");
            int xy=0;
    for (int in= 0; in < AfterSplit.length; in=in+2)
    {

       studentname[Integer.parseInt(AfterSplit[in+1])=AfterSplit[in];
       studentseat[Integer.parseInt(AfterSplit[in+1])="1";
   }

}

public void click(View v) {

    int w=0;
    switch(v.getId()) {

        case 0:
            if(checkpoint==1)
            {
                for(int i=0;i<6;i++){
                    ((ViewGroup) layout.getParent()).removeViewAt(0);}
                checkpoint=0;
                CurrentButtonNumber=1;
                break;

            }else{
                layout = new LinearLayout(this);
                                   sc = (ScrollView) findViewById(R.id.loo);
                mainLayout = (LinearLayout) findViewById(R.id.looo);
                layout.setOrientation(LinearLayout.HORIZONTAL);
                mainLayout.addView(layout);
                LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(150, 80);

                for (int j = 0; j < 5; j++) {
                    layout = new LinearLayout(this);
                    sc = (ScrollView) findViewById(R.id.loo);
                    mainLayout = (LinearLayout) findViewById(R.id.looo);
                    layout.setOrientation(LinearLayout.HORIZONTAL);
                    btn1 = new Button(this);

                    final TextView tx9=new TextView(this);

                    classtime++;
                    mainLayout.addView(layout);

                    for (int i = 0; i < 10; i++) {


             final ImageButton TmpBtn = new ImageButton(v.getContext());


          ------>if(studentseat[CurrentButtonNumber]=="1")<---------
                        {

                            TmpBtn.setImageResource(R.drawable.image1);

                        }else TmpBtn.setImageResource(R.drawable.image2);

这部分出错了 -------------&gt; if(studentseat[CurrentButtonNumber]=="1")

系统出现

"Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference"

我很确定&#34; Studentseat&#34;数组不为空。我在进行下一步TmpBtn.setOnClickListener时检查过,当我点击Textview时,该值可以显示在button中。哪部分出了问题?

0 个答案:

没有答案