【发布时间】:2013-04-10 15:21:51
【问题描述】:
当我在我的主类中添加构造函数时,由于下面的代码,它会给出错误。我能知道为什么吗? (没有构造函数它工作正常。)
public class AndroidGPSTrackingActivity extends Activity implements OnClickListener{
Button btnShowLocation ;
private final Context mContext;
public AndroidGPSTrackingActivity(Context context) {
this.mContext = context.getApplicationContext();
//tpMethod();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("Main", "class");
btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
// show location button click event
btnShowLocation.setOnClickListener(this);
}
}
错误:
java.lang.RuntimeException:无法实例化活动 ComponentInfo
java.lang.InstantiationException
【问题讨论】:
-
贴出整个logcat trace和相关代码。
-
java.lang.instantiationexception 无法实例化类没有空的构造函数
-
为什么还要向 Activity 添加构造函数?
-
@RaghavSood 实际上我想在另一个类中调用我的主类的方法,请关注我的上一篇文章。 stackoverflow.com/questions/15926742/…
标签: android constructor runtime-error