如何访问oncreate之外的arraylist

时间:2016-04-18 03:49:04

标签: java android arraylist

嗨,我是编程新手。我想从onCreate访问外部的arraylist,但我得到错误.Below是代码。

public class WelcomeOnFootActivity extends AppCompatActivity {
    ImageButton next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_on_foot);
        Bundle bundle=getIntent().getExtra;
        List<UserDate> data= new ArrayList();
        data.add((UserData)bundle.get("data"));
        next=(ImageButton)findViewById(R.id.next);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handle();
            }
        });
    }
private void handle(){
      String info=data.getUserInfo;// here i get error
}
}

3 个答案:

答案 0 :(得分:1)

只需在班级级别全局声明列表。在OnCreate之前的案例中。

List<UserDate> data= new ArrayList();
en@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome_on_foot);
    Bundle bundle=getIntent().getExtra;
    data.add((UserData)bundle.get("data"));
    next=(ImageButton)findViewById(R.id.next);

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            handle();
        }
    });


   }
  private void handle(){
      String info=data.getUserInfo;
}

希望它会有所帮助。

答案 1 :(得分:0)

public class WelcomeOnFootActivity extends AppCompatActivity {
    ImageButton next;
 List<UserDate> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_on_foot);
        Bundle bundle=getIntent().getExtra;
         data= new ArrayList();
        data.add((UserData)bundle.get("data"));
        next=(ImageButton)findViewById(R.id.next);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handle();
            }
        });
    }
private void handle(){
      String info=data.getUserInfo;// here i get error
}
}

答案 2 :(得分:0)

public class WelcomeOnFootActivity extends AppCompatActivity { 
    ImageButton next;

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome_on_foot);
        Bundle bundle=getIntent().getExtra;
       final List<UserDate> data= new ArrayList();
        data.add((UserData)bundle.get("data"));
        next=(ImageButton)findViewById(R.id.next);

        next.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                handle(data); 
            } 
        }); 
    } 
private void handle(List<UserDate> listUD){ 
      String info=listUD.getUserInfo;// here i get error
} 
}