在自定义控件中启动一个活动时出现的undefin的错误问题
来源:互联网 发布:java反射之getmethod 编辑:程序博客网 时间:2024/06/05 11:14
public class TitleLayout extends LinearLayout{
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
Button titleButton2 = (Button)findViewById(R.id.title_button2);
titleButton2.setOnClickListener(new OnClickListener() {@Override
public void onClick(View v) {
Intent intent = new Intent(TitleLayout.this,Setting.class);
startActivity(intent);
}
});
}
}
以上为错误代码,报错为:
The constructor Intent(TitleLayout, Class<Setting>) is undefined;
The method startActivity(Intent) is undefined for the type new View.OnClickListener(){};
问题就出在上下文上,还有就是 startActivity()是Context里的方法。
修改后的代码:
public class TitleLayout extends LinearLayout{
public TitleLayout(final Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
Button titleButton2 = (Button)findViewById(R.id.title_button2);
titleButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context,Setting.class);
context.startActivity(intent);
}
});
}
}
- 在自定义控件中启动一个活动时出现的undefin的错误问题
- 养成在活动中自定义启动该活动的方法 startAction( )的习惯
- 解决tomcat在eclipse中启动正常,但在登录主页时出现404错误的问题
- 在非启动项目中使用实体数据模型、wcf服务等控件时,经常出现的问题
- C# 在一个窗体中调用另一个窗体的控件可能出现的问题
- 自定义相机错误,出现无法启动的错误
- 自定义相机错误,出现无法启动的错误
- 在myeclipse中启动tomcat出现的一些问题详解
- 在AppCompatActivity中自定义toolbar出现的问题
- 自定义控件时使用setTextSize出现的问题
- 关于在自定义控件中事件不响应的问题
- 关于在自定义控件中事件不响应的问题
- 一个链表在使用时出现内存错误的问题
- 自定义控件的构造方法中用this出现的错误
- 活动被回收的处理、活动的启动模式、一键退出所有活动、UI控件、四种布局方式、自定义控件
- 关于Eclipse中Tomcat启动时出现的小错误
- ISE中启动modelsim时出现了下面的错误
- 关于自定义控件在布局文件中使用时提示error: Error parsing XML: not well-formed (invalid token)错误的问题
- Tensorflow object detection API 源码阅读笔记:基本类(1)
- 063day(快速排序和分治例题)
- python量化分系列之---使用tushare获取股票实时分笔数据延时有多大
- IT的道德与伦理
- org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### Cause: org.ap
- 在自定义控件中启动一个活动时出现的undefin的错误问题
- 学编程需要注意什么?
- 怎么通过反卷积生成和原始图像一样大小的图像? | 社区问答
- PM2实用入门指南
- 深度学习之星(二):GAN之图像转换 | 公开课
- Kaggle 比赛冠军经验分享:如何用 RNN 预测维基百科网络流量
- TensorFlow实现神经网络入门篇
- java socket调用webservice以及如何解决连接重置报错问题
- HashMap和HashTable的区别