2013-06-19 55 views
0

我想将片段活动中的数据传递给另一个类。但是, 它会产生一个错误,即“构造函数Intent(new View.OnClickListener(){},Class)未定义”。Android - 构造函数未定义错误

与此代码

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

这里是code..how我可以通过 '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!"); 
       } 
      }); 
     } 

回答

3

的意图constrcutor中的一种采用一个放慢参数和Context一个.class对象。所以你的this必须是一个有效的上下文或一个活动。从文档:

参数

  1. packageContext实施这一类的应用程序包的语境。
  2. cls要用于意图的组件类。

变化

new Intent(this, WebSocket_Connector.class); 

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

感谢。有用。 – user2500696

+0

欢迎你 – Blackbelt

+0

你可以请检查“Android - Autobahn Websocket发送消息错误(NullPointerException)”的问题,如果你不介意吗.. ..没有人回答.. :( – user2500696

0

你为什么不试试这个

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