Android的 - 构造函数是未定义错误 [英] Android - The constructor is undefined error
本文介绍了Android的 - 构造函数是未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要在片段的活动数据传递给另一个类。但,
它使一个错误,构造意图(新View.OnClickListener(){},类)是不确定的。
本code
'意向意图=新意图(这一点,WebSocket_Connector.class);'
下面是code..how我可以通过ID的字符串值?
公共类Myoffers_Fragment扩展片段{ 私人最终WebSocketConnection mConnection =新WebSocketConnection(); 公共静态的newInstance片段(Myoffers背景下,INT POS,浮动比例)
{
束B =新包();
b.putInt(POS,POS机);
b.putFloat(规模化,规模);
返回Fragment.instantiate(上下文,Myoffers_Fragment.class.getName(),二);
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
如果(集装箱== NULL){
返回null;
} 的LinearLayout L =(的LinearLayout)inflater.inflate(R.layout.mf,集装箱,FALSE); INT POS = this.getArguments()调用getInt(POS)。
TextView的电视=(TextView中)l.findViewById(R.id.text);
tv.setText(产品+ POS) 的ImageButton product_photo =(的ImageButton)l.findViewById(R.id.myoffer_image);
如果(POS == 0){
product_photo.setImageResource(R.drawable.myoffers_0);
product_photo.setOnClickListener(新ImageButton.OnClickListener(){
公共无效的onClick(视图v){
字符串ID1 =Product0;
意向意图=新意图(这一点,WebSocket_Connector.class);
intent.putExtra(KeyToAccessDataID1);
startActivity(意向);
Log.d(TAG,目前的产品是:+ ID1);
Log.d(TAG,ID1 +被发送到服务器!);
}
});
}
解决方案
的意图constrcutor之一,需要一个放慢参数上下文
和的.class
对象。所以,你的这个
必须有一个有效的上下文或活动。从文档:
参数
-
packageContext
实施这一类的应用程序包的上下文。 -
CLS
的组件类是用于意图。
修改
新意图(这一点,WebSocket_Connector.class);
与
新意图(getActivity(),WebSocket_Connector.class);
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".
with this code
'Intent intent = new Intent(this, WebSocket_Connector.class);'
Here is the code..how can I pass the 'id' string value?
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. So your this
has to be a valid context or an Activity. From the documentation:
Parameters
packageContext
A Context of the application package implementing this class.cls
The component class that is to be used for the intent.
Change
new Intent(this, WebSocket_Connector.class);
with
new Intent(getActivity(), WebSocket_Connector.class);
这篇关于Android的 - 构造函数是未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文