2014-01-09 46 views
0

您好我是新来的Android编程安卓:按钮点击后的错误传输数据

我试图创造一种,用户点击第一 页按钮,在按钮改变文字颜色的应用颜色和更改反映在另一个活动页面中。

为此,我有 1)一个片段类(BookLockerFragment),其参照包含按钮 2)父活动文件(TabActivity.java) 3)的活性文件以反映变化的XML文件( complainResponse.java)

下面是代码: LodgeComplaintFragment.java发生在这条线

ArrayList<String>userSelectedOptions = new ArrayList<String>(); 
if(btnSis.getCurrentTextColor()==Color.BLUE){ 
userSelectedOptions.add("SIS"); 
} 
Button but = (Button) root.findViewById(R.id.searchButton); 
    ..... 
    but.setOnClickListener(new View.OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
     buttonListener.onMakeBookingButtonPressed(userSelectedOptions); 
    } 
}); 

    TabMainActivity.java 
    public void onMakeBookingButtonPressed(ArrayList<String> list) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(TabMainActivity.this, 

        complainResponse.class); 
       intent.putStringArrayListExtra("userSelectOptions",list); 
       startActivity(intent); 
      } 


complainResponse.java 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Get the message from the intent 
    Intent intent = getIntent(); 
    setContentView(R.layout.complainresponse); 

    userInput = intent.getStringArrayListExtra("userSelectOptions"); 
    // Creates the window used for the UI 

    if (userInput != null) { 
     if (userInput.get(0) != null) { 
      textview1 = (TextView) findViewById(R.id.textView1); 
      textview1.setText(userInput.get(0)); 
     } 
    } 

} 

错误: 如果(userInput = NULL){ complainResponse.j的//! AVA

的logcat: java.lang.IndexOutOfBoundsException

请帮

+0

了大部分的代码已经被省略以允许相关的多个 – user2445971

+0

你从来没有把任何东西在列表中。 – hichris123

回答

0

没有什么在你传递给你的活动ArrayList中。 我怀疑是不是正在执行这段代码 -

if(btnSis.getCurrentTextColor()==Color.BLUE){ 
    userSelectedOptions.add("SIS"); <------------ never gets here 
} 

为了验证这一点,以调试模式运行应用程序,并放置一个断点在if声明

0
userInput.get(0) != null 

这是原因在我看来,错误列表可以被初始化但是是空的。

,而不是你应该使用,

if (!userInput.isEmpty())