简体   繁体   English

Android-构造函数未定义错误

[英]Android - The constructor is undefined error

I want to pass data in fragment activity to another class. 我想将片段活动中的数据传递给另一个类。 But, it makes an error that "The constructor Intent(new View.OnClickListener(){}, Class) is undefined". 但是,这会导致“未定义构造函数Intent(new View.OnClickListener(){},Class)”的错误。

with this code 用这个代码

'Intent intent = new Intent(this, WebSocket_Connector.class);'

Here is the code..how can I pass the 'id' string value? 这是代码。我如何传递“ id”字符串值?

public class Myoffers_Fragment extends Fragment {

    private final WebSocketConnection mConnection = new WebSocketConnection();

    public static Fragment newInstance(Myoffers context, int pos, float scale)
    {
        Bundle b = new Bundle();
        b.putInt("pos", pos);
        b.putFloat("scale", scale);
        return Fragment.instantiate(context, Myoffers_Fragment.class.getName(), b);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }

        LinearLayout l = (LinearLayout) inflater.inflate(R.layout.mf, container, false);

        int pos = this.getArguments().getInt("pos");
        TextView tv = (TextView) l.findViewById(R.id.text);
        tv.setText("Product " + pos);



        ImageButton product_photo = (ImageButton) l.findViewById(R.id.myoffer_image);


        if (pos == 0) {
            product_photo.setImageResource(R.drawable.myoffers_0);
            product_photo.setOnClickListener(new ImageButton.OnClickListener(){
                public void onClick(View v){
                    String id1 = "Product0";
                    Intent intent = new Intent(this, WebSocket_Connector.class);
                    intent.putExtra("KeyToAccessData", id1);
                    startActivity(intent);
                    Log.d(TAG, "Current product is : " + id1);
                    Log.d(TAG, id1 + "is sent to server!");
                }
            });
        }

one of the Intent constrcutor takes a paramter a Context and a .class object. Intent构造函数之一采用Context.class对象作为参数。 So your this has to be a valid context or an Activity. 因此, this必须是有效的上下文或活动。 From the documentation: 从文档中:

Parameters 参量

  1. packageContext A Context of the application package implementing this class. packageContext实现此类的应用程序包的上下文。
  2. cls The component class that is to be used for the intent. cls用于意图的组件类。

Change 更改

new Intent(this, WebSocket_Connector.class); 

with

new Intent(getActivity(), WebSocket_Connector.class);

你为什么不试试这个

Intent intent = new Intent(Myoffers_Fragment.this, WebSocket_Connector.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM